Home > Blockchain >  Having problems scanning a String from a file in Java
Having problems scanning a String from a file in Java

Time:03-25

I'm trying to scan a line of text from a .txt file, split it up into seven numbers, change two of the numbers, and then write the new numbers back into the .txt file. The code below works fine the first time, but seems to have issues with reading from the text file a second time for the new starting String. I've done very similar things multiple times and had no issues, so I'm really not sure why I'm having problems this time around. The code I currently have is:

public void addWin(int numGuesses) throws IOException {
    FileWriter writer = new FileWriter(*filepath*);
    Scanner scan = new Scanner(new File(*filepath*));
    String temp = "0;0;0;0;0;0;0;";
    if (scan.hasNextLine()) {
        temp = scan.nextLine();
    }
    String[] statsArr = temp.split(";");
    scan.close();
    statsArr[0] = Integer.toString(Integer.parseInt(statsArr[0])   1);
    statsArr[numGuesses] = Integer.toString(Integer.parseInt(statsArr[numGuesses])   1);
    for (int i = 0; i < statsArr.length; i  ) {
        writer.append(statsArr[i]   ";");
    }
    writer.close();
}

Some extra context if needed, this is essentially for a Wordle clone sort of thing for a Discord bot I have. numGuesses is the number of guesses it took to get the word correct. The String being written in and being read is 7 numbers divided up by a semicolon, the first number is the current win streak, the second number is number of times you've won in 1 guess, and so on. The testing I've done seems to place the error somewhere before the scanner closes. A first run through will correctly write the numbers, so if the word was guessed in 3 attempts the file will contain "1;0;0;1;0;0;0;", but the next time the method is called it essentially starts from scratch. Checking the temp variable just after the if statement on a second run through just shows "0;0;0;0;0;0;0;". Sorry for the long-windedness, just trying to provide all possibly helpful details. Thank you in advance!

-

CodePudding user response:

Consider the JavaDoc which states "Whether or not a file is available or may be created depends upon the underlying platform.". So what is happening here, is that when you use new FileWriter(*filepath*) the file is being locked/created blank, so when you use new Scanner(new File(*filepath*)); and scan.hasNextLine() you get a null/empty value.

The easy solution is to simply move the FileWriter further down in your code, and only open it after the scanner has been closed. Also add an else to your if statement so you know if there is an issue with reading from the scanner:

//Move the below line to be later in the code
//FileWriter writer = new FileWriter(*filepath*);
Scanner scan = new Scanner(new File(*filepath*));
String temp = "0;0;0;0;0;0;0;";
if (scan.hasNextLine()) {
    temp = scan.nextLine();
}
//Add some debugging
else{
    System.out.println("ERROR no data could be read");
}
String[] statsArr = temp.split(";");
scan.close();
statsArr[0] = Integer.toString(Integer.parseInt(statsArr[0])   1);
statsArr[numGuesses] = Integer.toString(Integer.parseInt(statsArr[numGuesses])   1);

//Create the flie writer here instead
FileWriter writer = new FileWriter(*filepath*);
for (int i = 0; i < statsArr.length; i  ) {
    writer.append(statsArr[i]   ";");
}
writer.close();

Now assuming the file exists and can be edited, and where numGuesses = 3, then for the following contents:

1;2;3;4;5;6;7;

The output of running the code is as expected ( 1 to the 0 and 3rd index)

2;2;3;5;5;6;7;

The reason you only saw 0;0;0;0;0;0;0; was because the code was failing to read from the scanner, and always using the temp value from this line String temp = "0;0;0;0;0;0;0;";. By adding the else check above we can see when it fails.

CodePudding user response:

FileWriter writer = new FileWriter(*filepath*); clears the contents of the file you are trying to read from. You need to move this line after scan.close();

  • Related