javafileline-by-line

How to avoid to add one empty char at beginning while reading a file in Java?


I have several dictionary files that i read in Java and while reading them line by line, i use this code:

       public static void main(String args[]) { 


    try {

        FileInputStream fstream1 = new FileInputStream("de-DE.dic");
                    DataInputStream in = new DataInputStream(fstream1);
                BufferedReader br = new BufferedReader(new InputStreamReader(in,"UTF-8"));

      String str;
      while ((str = br.readLine()) != null) {
         String str_uc=str.toUpperCase(Locale.GERMAN);
          if(hasApostrophe(str_uc)){
              allletters.add(str_uc);
          if(str.length()==3)
              threeletter.add(str_uc);
          else if(str.length()==4)
              fourletter.add(str_uc);
          else if(str.length()==5)
              fiveletter.add(str_uc);
          else if(str.length()==6)
              sixletter.add(str_uc);
          else if(str.length()==7)
              sevenletter.add(str_uc);
       }
      }
      in.close();

    }
    catch (Exception e) {
      System.err.println(e);
    }

However, it always add one empty char to the first line word and for example if three letter word is in first line,it is added to fourletter array. How can i prevent this happening? Thanks.

ADDITION:

Here is a few lines from the file:

Aachens
Aachen
Aal
Aale
Aalen
Aales
Aals
Aas
Aases
Aasgeier
Aasgeiern
Aasgeiers

Solution

  • EDIT: Its a note pad issue. Read this

    Use

    String str_uc=str.trim().toUpperCase(Locale.GERMAN);
    

    trim() will remove whitespace characters at the start or end of the line.