0

So i want to read a String and an int from a text file and it gets me java.lang.ArrayIndexOutOfBoundsException: 1

public class GetNameAndNumber{
    private ArrayList <NameAndNumber> list = new ArrayList <NameAndNumber>();
    BufferedReader buf = new BufferedReader(new FileReader("NameAndNumber.txt"));
    String linie = buf.readLine();
    while(true)
            {
            linie = buf.readLine();
            if(linie == null)
                break;
            else
                {
                String split[] = linie.split(" ");
                NameAndNumber nan = new NameAndNumber(split[0], Integer.parseInt(split[1]));
                list.add(nan);
                }
            }

}

the "NameAndNumber" class has a String and an int

and this is the text file:

John 1
David 0
Ringo 55

What i don't know is why this one gives me an error, but when i read 2 strings and then an int like

NameAndNumber nan = new NameAndNumber(split[0], split[1], Integer.parseInt(split[2])); - this "NameAndNumber" having two strings and an int

for a text file like

Johnny John 8
Mathew John 0

it gives me no errors and stores the values correctly. why ?

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
JohnnySmith88
  • 162
  • 1
  • 10
  • The only explanation I can conjure here is that one (or more) of your text lines does _not_ have two strings separated by a space. – Tim Biegeleisen Jan 24 '19 at 19:55
  • 2
    Or there is one more explanation.You might have additional empty lines at the end of your file. In this case you will see `Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1` And there is also one problem in your code. First line of your text file will be omitted because you call `buf.readLine();` two times before parsing a content – Planck Constant Jan 24 '19 at 19:59
  • Can you post the stack trace for the error? – Abra Jan 24 '19 at 20:06
  • you are right Uata. the empty spaces were my problem. that drove me nuts. thank you both and also thank you Uata for telling me why the first line is omitted, yeah that was also a problem. – JohnnySmith88 Jan 24 '19 at 20:08
  • @JohnnySmith88: you're welcome:) Happy coding – Planck Constant Jan 24 '19 at 20:09

3 Answers3

1

You most likely have a line without 2 strings on it. Maybe a blank line at the end of the file. I would suggest adding code to display each line right after you read it in, for debugging purposes. Then you can see where the bad line is in your input file.

linie = buf.readLine();
System.out.println("line: '" + linie + "'");

You could add additional code to skip lines that don't have two strings.

String split[] = linie.split(" ");
if (split.length < 2) continue; // skip bad input
Sean N.
  • 963
  • 2
  • 10
  • 23
1

I am assuming in you first file you have some empty spaces, because I ran your code and it worked fine. I made it fail by adding some empty spaces at the end with the Index out of exception. One thing you can do it add some checks as follows by doing a trim and checking if the line is empty.

linie = buf.readLine().trim(); if(linie == null || linie.isEmpty()) break;

togise
  • 278
  • 3
  • 9
0

Try this

    for(;;) {
        String line = bufferedReader.readLine();
        if (line == null) {
          break;
        }
   }
Ng Sharma
  • 2,072
  • 8
  • 27
  • 49