0

I have broken down my code and reviewed it 1 by 1 to see this error and I am not sure of how to handle it, I'm trying to read multiple lines from file and store the data into Array. I'm able to store details[0] as Username but when I try adding details[1] and details[2] I get the error. Can someone help me please?

Data in the file:

test|9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08|500
testing|9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08|400
testtry|9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08|300
testnew|9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08|200

The code is below

    Scanner read = new Scanner(new File("players.dat"));
    String line;

    ArrayList<String> players = new ArrayList<String>();
    while (read.hasNextLine()){
        line = read.nextLine();
        String[] details = line.split("\\|");
        String UserN = details[0];
        String Password = details[1];
        String Chips = details[2];
2
  • Is there an empty file at the end? Commented Apr 11, 2015 at 11:43
  • i am not sure of what u mean by empty file at the end but... when i split details[0] becomes all the data leaving details[1] to be empty Commented Apr 11, 2015 at 11:49

2 Answers 2

6

You're probably reading a blank line. Never split without checking after that that the item exists.

Try to print the line you're splitting to see why you're getting the error. Tip for life: Always use the debugger, it's there for you.

Note: It's good that you escaped the | character, as split takes a regex, and | regex means: "empty string, or.. an empty string".

Sign up to request clarification or add additional context in comments.

3 Comments

ok i check by printing the line, it looks ok to me, i think when i add to array i might have encountered a new line which is blank
Debug your code, check the content of line on each iteration. I can't see any other reason why you're having this exception.. (Unless the exception is from a different array) Otherwise, that's a sad day for humanity, computers had conquered the world.
Hi, i think i know the error , when it read details[1], it becomes blank. it seems that details[0] becomes all the 3 split String
0

(This is an extended comment - unfortunately, it is not possible to put readable code in an actual comment.)

The bug is somewhere other than the code and data in the question.

The following is a simple program that applies the given code to the given data. It does not reproduce the problem. I strongly support the recommendation for further debug, and also suggest trying to construct an SSCCE.

import java.io.Reader;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.Scanner;

public class Test {
  public static void main(String[] args) {
    String inputData = "test|9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08|500\n"
        + "testing|9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08|400\n"
        + "testtry|9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08|300\n"
        + "testnew|9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08|200\n";
    Reader inputReader = new StringReader(inputData);
    Scanner read = new Scanner(inputReader);
    String line;

    ArrayList<String> players = new ArrayList<String>();
    while (read.hasNextLine()) {
      line = read.nextLine();
      String[] details = line.split("\\|");
      String UserN = details[0];
      String Password = details[1];
      String Chips = details[2];
      System.out.printf("UserN=%s, Password=%s, Chips=%s%n", UserN, Password,
          Chips);
    }
    read.close();
  }
}

Output:

UserN=test, Password=9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08, Chips=500
UserN=testing, Password=9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08, Chips=400
UserN=testtry, Password=9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08, Chips=300
UserN=testnew, Password=9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08, Chips=200

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.