0

I have a Student class and in the main class I have a method which takes a fileName(the file contains student records with names and marks of subjects) as input and returns an array of Student as output.

I tried using ArrayList however I am stuck converting the student's record to student array. Please help..Below is my code:

public static void main(String[] args) throws FileNotFoundException {
    String filename = "student.txt";
    readData(filename);
}

private static Student[] readData(String filename) throws FileNotFoundException{
    Scanner input = new Scanner(new File(filename));

    ArrayList<Student> list = new ArrayList<Student>();
    while(input.hasNext()){
        String name = input.next();
        int physic = input.nextInt();
        int chemistry = input.nextInt();
        int math = input.nextInt();
        Student studentR = new Student(name,physic,chemistry,math);
        list.add(studentR);
        System.out.println(list);
    }
    for (Student studentR : list) {
        System.out.println(studentR);
        return studentR;
    }
}

And my text file looks like:

Amy 85 95 78
Nancy 83 93 82
Richard 81 91 86
Daniel 77 78 79
James 80 90 85
Cathy 95 74 89
Paul 84 87 79

Below is the error I am getting:

Main.java:28: error: incompatible types: Student cannot be converted to Student[]
                        return studentR;
                               ^
Vüsal
  • 2,580
  • 1
  • 12
  • 31
mehsgj
  • 1
  • 1
  • 2
    Possible duplicate of [From Arraylist to Array](https://stackoverflow.com/questions/7969023/from-arraylist-to-array) – George Z. Sep 28 '19 at 06:43
  • 1
    Or you can make your method return a List: `private static List readData(String filename)` – George Z. Sep 28 '19 at 06:43
  • @mehsgj First convert the list to array and return it. You can use this post https://stackoverflow.com/questions/5374311/convert-arrayliststring-to-string-array to see how to convert list to array. And you should return it after the for loops at the end of the method or just change the return type of the function to List instead of array like George Z say. –  Sep 28 '19 at 06:48
  • And you don’t want to return from inside the loop – Joakim Danielson Sep 28 '19 at 06:48

2 Answers2

0

Try this code:

 public static void main(String[] args) throws FileNotFoundException {
     String filename = "student.txt";
     readData(filename);
 }

 private static List < Student > readData(String filename) throws FileNotFoundException {
     Scanner input = new Scanner(new File(filename));
     List < Student > list = new List < Student > ();

     while (input.hasNext()) {
         String name = input.next();
         int physic = input.nextInt();
         int chemistry = input.nextInt();
         int math = input.nextInt();
         Student studentR = new Student(name, physic, chemistry, math);
         list.add(studentR);
         System.out.println(list);
     }

     for (Student studentR: list) {
         System.out.println(studentR);
     }

     return list;
 }
Andrew Regan
  • 5,087
  • 6
  • 37
  • 73
Mr Freak
  • 216
  • 3
  • 20
  • I tried converting my list to array and printing it but it prints the array address Student@266474c2 Student@6f94fa3e Student@5e481248 Student@66d3c617 Student@63947c6b Student@2b193f2d Student@355da254 Student@4dc63996 Student@d716361 Student@6ff3c5b5 Student@3764951d Student@4b1210ee Student@4d7e1886 Student@3cd1a2f1 Student@2f0e140b Student@7440e464 Student@49476842 Student@78308db1 Student@27c170f0 Student@5451c3a8 – mehsgj Sep 28 '19 at 09:26
  • Also i should have to return an student array not list – mehsgj Sep 28 '19 at 09:28
0

Firstly, you should change your return type to List as you have your data in ArrayList and not just Array. Secondly, returning from onside for loop is not what you would want, it needs to be outside for loop. And lastly, an object wont get printed just with writing object name inside system.out.print, you need to print the element by accesing its item like studentR.name, studentR.marks etc