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:


Instead I am getting this:


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.

Re: Extracting from a file 80 80

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.

Re: Extracting from a file 80 80

That was it. Thank you Ezzaral :)

Be a part of the DaniWeb community

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