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.

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.

That was it. Thank you Ezzaral :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.