I don't know why it won't work. I've double and triple checked that the file in FileWriter has text (inputted by another PrintWriter earlier in a separate program) but the while statement doesn't seem to run. The commented out lines were various tests I was running to try to figure out what was going on. What I'm trying to work out is have it iterate through the array and add a group ID to all Persons. If anyone knows what's up, it would be greatly appreciated. I'll apologize in advance for any formatting errors, any comments on how to be more easily helped would also be greatly appreciated.
public static void updateWinners(Person[] Players, int n)
throws FileNotFoundException {
// n is 2 or 4 depending on round
File fileS = new File(
"C:\\Users\\Patrick\\Desktop\\New folder\\FileWriter\\Win");
File fileP = new File(
"C:\\Users\\Patrick\\Desktop\\New folder\\Bracket\\Win");
Scanner fs = new Scanner(fileS);
PrintWriter writer = new PrintWriter(fileP);
//int q=0;
while (fs.hasNextLine()) {
//System.out.println(Players[q].toString());
for (int i = 0; i < Players.length; i++) {
if (fs.nextLine().equals(Players[i].toString())) {
Players[i].addGroup(alpha[i / n]);
System.out.println(Players[i].toString());
writer.println(Players[i].toString());
}
}
//q++;
}
writer.close();
fs.close();
}
Do it like this,
while (fs.hasNextLine()) {
String s = fs.nextLine();
//System.out.println(Players[q].toString());
for (int i = 0; i < Players.length; i++) {
if (s.equals(Players[i].toString())) {
Players[i].addGroup(alpha[i / n]);
System.out.println(Players[i].toString());
writer.println(Players[i].toString());
}
}
//q++;
}
Explanation:
When you call fs.nextLine()
each time in the players loop. It reads a new line from the file. So All lines of the file are read before you complete all the players.
Scanner throws exception when you try to read and there are no more data in the file.
Throws:
NoSuchElementException - if no line was found