java - Scanner - Ignore new line at end of file -


i need quitting when coming to/ignoring last new line in file reg.txt. of right error when reaches last line, containing nothing.

public string load() {         list.removeallelements();         try {             scanner scanner = new scanner(new file("reg.txt"));              while (scanner.hasnextline()) {                 string lastname = scanner.next();                 string firstname = scanner.next();                 string number = scanner.next();                 list.add(new entry(firstname, lastname, number));             }             msg = "the file reg.txt has been opened";             return msg;         } catch (numberformatexception ne) {             msg = ("can't find reg.txt");             return msg;         } catch (ioexception ie) {             msg = ("can't find reg.txt");             return msg;         }     } 

sample reg.txt:

allegrettho     albert          0111-27543 brio            britta          0113-45771 cresendo        crister         0111-27440 

how should edit scanner-reading ignores new line @ end of file?

at end of loop, do

scanner.nextline(); 

Comments

Popular posts from this blog

c# - How to get the current UAC mode -

postgresql - Lazarus + Postgres: incomplete startup packet -

javascript - Ajax jqXHR.status==0 fix error -