I am trying to read a line from a file, then print only the first word in the line. It needs to do this until the end of the file.

The file is as follows:

Bib 0.9898 .iuiu k.kljlkj .98908
Joel .0909 .iuou k.iopi .jlkj
...

I need it to display as follows:

Bib
Joel...

Instead I am getting this:

Bib
Bibl
Biblj...

It keeps adding on to the first name instead of incrementing to the next line. Any suggestions would be great.
My snippet is as follows:

 while ((strLine = br.readLine()) != null)
            {
               while(strLine.charAt(index) != ' ')
               {
                  tmp += strLine.charAt(index++);
               }            
               System.out.println (tmp);
            }

Thanks for any help.

Edited 3 Years Ago by mike_2000_17: Fixed formatting

You need to reset index and tmp before scanning each line. Also you don't have any checks on index vs strLine.length() so if you did have a line with no spaces in it you will get an index out of range.

This question has already been answered. Start a new discussion instead.