So normally, I know one would use a standard for loop to iterate though the array, like such:
public static void readCodesFromFile(String filename, String[] codes) throws FileNotFoundException {
try ( Scanner fin = new Scanner(new File(filename) ); ) {
for (int i = 0; i <= codes.length - 1; i++) {
codes[i] = fin.nextLine();
}
}
}
But, I desperately want to discover how to do this with a for each loop instead. At this point I just need to know if it can be done, but also would love the efficiency and cleanliness of using this loop for this task. I have tried something like this, and moved things around many times, but cannot seem to get anything to read properly.
Below is the for each ( : ) I have been working with:
public static void readCodesFromFile(String filename, String [] codes) throws FileNotFoundException {
try (Scanner fin = new Scanner(new File(filename) ); ) {
for (String code : codes) {
codes = fin.nextLine();
}
}
}