public static void findAllStudent(String fileName) throws IOException { ArrayList<Student> array = new ArrayList<>(); ReadStudent(fileName, array); //判断集合为空的时候,要给个提示 if (array.size() == 0) { System.out.println("暂时没有学生可供查询,请重新选择"); return; } System.out.println("学号\t\t姓名\t年龄\t居住地"); //遍历集合 for (Student student : array) { System.out.println(student.getId() + "\t" + student.getName() + "\t" + student.getAge() + "\t\t" + student.getAddress()); } WriteStudent(fileName, array); }哪里错了
时间: 2024-04-20 18:24:38 浏览: 40
根据您提供的代码,目前没有明显的错误。但是,为了更好地帮助您找到潜在的问题,我需要更多的信息。请提供以下信息:
1. 错误信息:您是否收到任何错误消息或异常?如果是,请提供详细的错误信息。
2. ReadStudent() 和 WriteStudent() 方法:这些方法在代码中没有提供,您能提供这些方法的实现吗?
3. 输入文件:您正在尝试读取和写入的文件是什么类型的文件?请确保文件存在且具有正确的权限。
请提供这些额外的信息,以便我能够更好地帮助您找到问题所在。
相关问题
Task 2. Create a list of students by reading from student_hobbies.txt and display the list. // - You will also need to create a Student class, which has at least // two data fields: student name and an ArrayList of hobbies (String-type).优化以下代码private static ArrayList<Student> readFile(String string) throws IOException{ ArrayList<Student> studentlist=new ArrayList<Student>(); File file = new File("student_hobbies_JU.txt"); Scanner scanner = new Scanner(file); while (scanner.hasNextLine()) { String line = scanner.nextLine(); String[] data = line.split(", "); System.out.println(data); String name = data[0]; ArrayList<String> hobbies = new ArrayList<String>(); for(int i=0;i<data.length;i++){ hobbies.add(data[i]); } Student student = new Student(name, hobbies); studentlist.add(student); System.out.println(student); } scanner.close(); return studentlist; }//end readFile
下面是优化后的代码,其中将文件名作为参数传入函数中,使函数更加通用:
```
private static ArrayList<Student> readFile(String fileName) throws IOException {
ArrayList<Student> studentList = new ArrayList<Student>();
File file = new File(fileName);
Scanner scanner = new Scanner(file);
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
String[] data = line.split(", ");
String name = data[0];
ArrayList<String> hobbies = new ArrayList<String>();
for (int i = 1; i < data.length; i++) { // 从第二个元素开始,将所有的兴趣爱好添加到hobbies列表中
hobbies.add(data[i]);
}
Student student = new Student(name, hobbies);
studentList.add(student);
}
scanner.close();
return studentList;
}
```
该函数将指定文件中的内容读取到一个ArrayList<Student>中,并返回该列表。下面是Student类的代码:
```
public class Student {
private String name;
private ArrayList<String> hobbies;
public Student(String name, ArrayList<String> hobbies) {
this.name = name;
this.hobbies = hobbies;
}
public String getName() {
return name;
}
public ArrayList<String> getHobbies() {
return hobbies;
}
public void setName(String name) {
this.name = name;
}
public void setHobbies(ArrayList<String> hobbies) {
this.hobbies = hobbies;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", hobbies=" + hobbies +
'}';
}
}
```
该类包含两个数据字段:name和hobbies,分别表示学生的姓名和兴趣爱好列表。该类还包含构造函数、getter和setter方法以及toString方法,用于打印学生对象的信息。可以使用以下代码调用readFile函数并打印学生列表:
```
try {
ArrayList<Student> studentList = readFile("student_hobbies.txt");
for (Student student : studentList) {
System.out.println(student);
}
} catch (IOException e) {
e.printStackTrace();
}
```
static Scanner sc = new Scanner(System.in); static File file=new File("D:\\HeimaIT\\OtherData\\lycode\\javase\\takeawaysystem2\\src\\com\\itheima\\dao\\impl\\ResterauntInfo.txt"); static ArrayList<Resteraunt> resteraunts = new ArrayList<>(); static ArrayList<Menu> orderList = new ArrayList<Menu>(); static ArrayList<Menu> menus = new ArrayList<>(); OrderService orderService=new OrderServiceImpl(); static OrderDaoImpl orderDao = OrderDaoImpl.getInstance(); //单例设计,返回永远的一个resterauntDao对象 static ResterauntDaoImpl resterauntDao=new ResterauntDaoImpl(); private ResterauntDaoImpl() { } static { //加载读取orderDao层的arraylist。用来后面的餐厅加载 ArrayList<ArrayList<Menu>> arrayLists = orderDao.readOrder(); Resteraunt resteraunt = new Resteraunt("家常菜馆1", arrayLists.get(0)); Resteraunt resteraunt2 = new Resteraunt("平价菜馆2", arrayLists.get(1)); Resteraunt resteraunt3 = new Resteraunt("高贵菜馆1", arrayLists.get(2)); Collections.addAll(resteraunts, resteraunt, resteraunt2, resteraunt3); try ( ObjectOutputStream objectOutputStream = new ObjectOutputStream(new FileOutputStream(file)); ){ objectOutputStream.writeObject(resteraunts); } catch (Exception e) { e.printStackTrace(); } } public static ResterauntDaoImpl getInstance(){ return resterauntDao; } public ArrayList<Resteraunt> readResterauntInfo() throws IOException, ClassNotFoundException { ObjectInputStream objectInputStream = new ObjectInputStream(new FileInputStream(file)); Object o = objectInputStream.readObject(); ArrayList<Resteraunt> o1 = (ArrayList<Resteraunt>) o; return o1; }
这段代码是一个餐厅信息管理系统的实现,其中包括了读取餐厅信息、订单信息,获取单例对象等操作。具体来说:
- 餐厅信息通过序列化的方式存储到ResterauntInfo.txt文件中,使用ObjectInputStream读取信息,并返回ArrayList<Resteraunt>类型的餐厅信息。
- 订单信息通过OrderDaoImpl实现,读取Order.txt文件中的订单信息,并返回ArrayList<ArrayList<Menu>>类型的订单信息。
- 通过单例设计模式,保证ResterauntDaoImpl的唯一性。
- 在静态代码块中,读取OrderDaoImpl中的订单信息,并根据这些信息创建三个Resteraunt对象,并将这些对象添加到resteraunts列表中,最后将该列表序列化到ResterauntInfo.txt文件中。
- 定义了ArrayList<Menu>类型的orderList和menus列表,以及OrderService类型的orderService对象,用于后续的订单管理。
阅读全文