0

Just at it says in the title, I can save something inside the rtf file sometimes(my save function is a working progress). Im using a mac, and i cant save as a txt. I do not know if this is normal or not and I having troubles with it since I dont know why but I get an arrayoutofbounds error when I try to use the line Player p = new Player(splitLine[0], splitLine[1], splitLine[2]);

Here is my read and write code.

final String FILE_PATH = "/Users/macbookair/Desktop/comp sci ia/TypingPractice/Player records.rtf";
BufferedReader reader;
PrintWriter writer;
Player[] readRecords() {
    // This is called by AppLogic.load() which runs when the AppLogic is
    // instantiated.

    // The array of Person objects that we create from the load file
    // This holds the current line from the load file
    String nextLine;
    // This is a two-element array that holds name/surname once it has
    // been split at the # sign
    String[] splitLine = new String[4];
    for(int i=0; i< splitLine.length;i++){
        splitLine[i] = "0";
    }
    // This is just a counter of how many lines I've read in
    int count = 0;

    try {
        reader = new BufferedReader(new FileReader(FILE_PATH));
        // Get the first line
        nextLine = reader.readLine();
        // Loop until we've been through every line in the file
        while (nextLine != null) {
            // Split the current line at the # sign
            splitLine = nextLine.split("#");
            Player p = new Player(splitLine[0], splitLine[1], splitLine[2]);
            // Put it in the array
            playerArray[count] = p;
            // Increment the counter
            count = count + 1;
            // Get the next line
            nextLine = reader.readLine();
        }
        reader.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;


}

void writeRecords(Player[] p) {

    try {
        writer = new PrintWriter(new FileWriter(FILE_PATHPhrase));
        // Loop through each Person in the Person array
        for (int i = 0; i < p.length; i++) {
            // Write the name, then a # sign, then the surname
            writer.println(p[i].getWPM() + "#" + p[i].getMistakes() + "#" + p[i].getTime());

        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    writer.close();
}
quak
  • 11
  • 2
  • 1
    *save* is an entirely meaningless tag. It's always a good idea to add a tag for the language you're coding in, as it gets the question in front of the people who are familiar with that language. – Ken White Jan 27 '16 at 00:42
  • It's possible that `reader.close()` is not being called if an error is thrown, so put that in your catch block. – AMACB Jan 27 '16 at 00:53
  • The line `splitLine = nextLine.split("#");` throws away the previous value of splitLine (which was initialized as an array of 4 Strings) and assigns it to a totally new array which is returned by the `split` method. There was no point in initializing it in the first place. The new array has fewer than 3 elements (because there are fewer than two `#` separators in the line), hence the exception when you try to use splitLine[2]. – Klitos Kyriacou Jan 27 '16 at 00:56

0 Answers0