I am trying to read several doubles from a file and save them in an array. The first number in the file is an integer, which should then determine the size of the array. This works. but when I try to read in the doubles afterwards, I get a false return and it skips the loop. what could be the reason for this?
Heres the method I use for reading the file
public static void readFile()
{
System.out.println("Filename (Has to be on Desktop): ");
Scanner scan = new Scanner(System.in);
String fileName = scan.nextLine();
String filePath = searchFileOnDesktop(fileName + ".txt");
try
{
File file = new File(filePath);
Scanner reader = new Scanner(file);
int arraySize = reader.nextInt();
double[] data = new double[arraySize];
//reader.next();
while (reader.hasNextDouble())
{
int i = 0;
data[i] = reader.nextDouble();
System.out.println(data[i]);
i++;
}
reader.close();
}
catch (FileNotFoundException e)
{
System.out.println("Datei konnte nicht geöffnet werden.");
e.printStackTrace();
}
}
I have already tried to “reset” the reader with .next(). at least that’s what I read somewhere. I can’t read it in as an integer, string or whatever else there is
Szihuma is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1