Why am I getting a StringIndexOutOfBoundsException when I try to skip multiple lines with BufferedReader?

LuminousNutria :

I am working on a game, and I want to use this text file of mythological names to procedurally generate galaxy solar-system names.

When I read the text file, I tell the while-loop I'm using to continue if there is something that's not a name on a given line. That seems to throw an exception in some (not all) areas where there are multiple lines without names.

How can I make the program work without throwing exceptions or reading lines without names on them?

My Code:

public class Rewrite {

public static void main(String[] args) {

   loadFromFile();
}

private static void loadFromFile() {

   String[] names = new String[1000];

   try {

      FileReader fr = new FileReader("src/res/names/Galaxy_System_Names.txt");
      BufferedReader br = new BufferedReader(fr);

      String aLine;
      int countIndex = 0;
      while ((aLine = br.readLine()) != null) {

         // skip lines without names
         if (aLine.equals(String.valueOf(System.lineSeparator()))) {
            aLine = br.readLine();
            continue;
         } else if (aLine.equals("&")) {
            aLine = br.readLine();
            continue;
         } else if (aLine.startsWith("(")) {
            aLine = br.readLine();
            continue;
         }

         System.out.println(aLine);

         // capitalize first letter of the line
         String firstLetter = String.valueOf(aLine.charAt(0));
         aLine = firstLetter + aLine.substring(1);

         names[countIndex++] = aLine;
      }

      br.close();

   } catch (IOException e) {

      e.printStackTrace();
   }
}
}

The Exception Thrown:

Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 0
    at java.base/java.lang.StringLatin1.charAt(StringLatin1.java:47)
    at java.base/java.lang.String.charAt(String.java:702)
    at utilities.Rewrite.loadHumanNamesFromFile(Rewrite.java:39)
    at utilities.Rewrite.main(Rewrite.java:10)

Text-File sample: This throws an error after the name "amor"

áed
áedán
aegle
aella
aeneas
aeolus
aeron
(2)

&
aeson
agamemnon
agaue
aglaea
aglaia
agni
(1)
agrona
ahriman
ahti
ahura
mazda
aias
aigle
ailill
aineias
aino
aiolos
ajax
akantha
alberic
alberich
alcides
alcippe
alcmene
alcyone
alecto
alekto
alexander
alexandra
alexandros
alf
(1)
alfr
alkeides
alkippe
alkmene
alkyone
althea
alvis
alvíss
amalthea
amaterasu
amen
ameretat
amirani
ammon
amon
amon-ra
amor

&
amordad
amulius
amun
GBlodgett :

From the docs of the BufferedReader::readLine:

Returns: A String containing the contents of the line, not including any line-termination characters

Thus when you get to this part of the file:

amor

&

It will read the blank line and strip the linebreak character, and all that will be left is an empty String. Therefore it will not be caught by your if statement:

if (aLine.equals(String.valueOf(System.lineSeparator())))

You need to add in a check for isEmpty()

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=155281&siteId=1