I'm just starting to learn Java and I'm trying to complete this exercise.
I've understood how to extract the information from the txt file (I think) using scanner (we're only supposed to change the method body). However I'm not sure of the correct syntax to transfer the information to an array.
I realise it must be very simple, but I can't seem to figure it out. Could someone please point me in the right direction in terms of syntax and elements needed? Thank you in advance!
import java.util.Scanner;
import java.io.FileReader;
import java.io.IOException;
public class Lab02Task2 {
/**
* Loads the game records from a text file.
* A GameRecord array is constructed to store all the game records.
* The size of the GameRecord array should be the same as the number of non-empty records in the text file.
* The GameRecord array contains no null/empty entries.
*
* @param reader The java.io.Reader object that points to the text file to be read.
* @return A GameRecord array containing all the game records read from the text file.
*/
public GameRecord[] loadGameRecord(java.io.Reader reader) {
// write your code after this line
Scanner input = new Scanner(reader);
for (int i=0; input.hasNextLine(); i++) {
String inputRecord = input.nextLine();
input = new Scanner(inputRecord);
// array?
}
return null; // this line should be modified/removed after finishing the implementation of this method.
}
}
In case you already have a
String
of the file content, you can say: