Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Getting an error while trying to split two tokens

I keep getting an out of bounds exception error when adding a second Entry variable.
The file that I’m trying to read in has a last line that just says "END"
the rest of the lines are formatted such as

1839,Sutter's Fort founded

and the file ends with:

END
      while (myReader.hasNextLine()) {
        String string = myReader.nextLine();
        if (string == "END") break;
         String[] parts = string.split(",");
        String part1 = parts[0];
        String part2 = parts[1];
         Entry a = new Entry(part1, part2);
         array[10].addHead(a);          // printing out the values
         System.out.println(part1 + " " + part2);
         System.out.println(array[10].removeHead().key);
      }

I tried adding a break statement if the String were to equal "END" but it seems as though it either won’t break out or just ignores it.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You need to use String.equals str == otherStr only returns true if both sides are the exact same object

 while (myReader.hasNextLine()) {
    String string = myReader.nextLine();
    if (string.equals("END")) break;
     String[] parts = string.split(",");
    String part1 = parts[0];
    String part2 = parts[1];
     Entry a = new Entry(part1, part2);
     array[10].addHead(a);          // printing out the values
     System.out.println(part1 + " " + part2);
     System.out.println(array[10].removeHead().key);
  }
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading