I am trying to read in 10 signed integers from a file into an array and for some reason, it is not happening and I am not getting any errors at compile and runtime. I just wanted a second pair of eyes to look over this and see what I might be missing.
The test file is “input.txt” and contains: -1, 4, 32, 0, -12, 2, 30, 1, -3, -32
Here is my code:
public void readFromFile(String filename)
{
try {
File f = new File(filename);
Scanner scan = new Scanner(f);
String nextLine;
int[] testAry = new int[10];
int i = 0;
while (scan.hasNextInt())
{
testAry[i] = scan.nextInt();
i++;
}
}
catch (FileNotFoundException fnf)
{
System.out.println(fnf.getMessage());
}
}
Your using the default delimiter on the Scanner object.
try using the delimiter ive got in the line useDelimiter(\\s*,\\s*”). Its regex to split up your the input from the file by a comma.