java.util.scanner - Java Scanner nextLong -


stackoverflow, hello. have problem understanding output piece of code

public static void main (string[] args) {     string context = "0100 55 3l 62d 127 6f (int)7";     scanner s = new scanner(context);     while(s.hasnextlong()) {         system.out.print(s.nextlong() + ",");     } } 

the output 100,55, don't understand why 127 wasn't included output? mean why output isn't 100,55,127, ?

scanner#nextlong() doesn't parse long literals, parses numbers longs, fails find 1 @ 3l , exits loop, ie. hasnextlong() returns false.

note javadoc

scans next token of input long.

in case next token 3l , doesn't parse long. try it

long.parselong("3l"); 

will give numberformatexception.


Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -