I'm doing an assignment where I need to read the file, the input of this method should be a file name and the output should be an array of students, the signature method is:
private static Student[] readData(String filename);
My current code is:
import java.io.*;
import java.io.File;
import java.util.Scanner;
class Main{
public static void main(String[] args) {
String []text = readData("students.txt");
for(int i = 0; i<text.length; i++){
System.out.println(text[i]);
}
}
private static Student[] readData(String filename){
String[] data = "";
try{
File myObj = new File(filename);
Scanner myReader = new Scanner(myObj);
while(myReader.hasNextLine()){
data =myReader.nextLine();
System.out.println(data);
}
}catch (FileNotFoundException e){
System.out.println("Error");
e.printStackTrace();
}
return data;
}
}
When I compile the code, it said error "incompatible types: Student[] cannot be converted to String[]". Can you guys explain to me what happens here and how can I fix it?