while (scanner.hasNext()) loop not working twice? Java -


i have made program vending machine!

my code similar to:

public static void main (string [] args) {     scanner sc = new scanner (system.in);     while(sc.hasnext()) {         string string = sc.next();          sum = generatesum(sum)         .....     } }  public static int generatesum(int sum) {     scanner sc = new scanner (system.in);     while (sc.hasnext()) {         ....     }      return sum; } 

sorry simplifying code, normal 1 long! however, problem use while (sc.hasnext()) loop twice. want continue main method until input user terminate, program terminates after running once.

i figured if take out generatesum method, loop in main method works fine guess has have while (sc.hasnext()) loop twice.

any ideas how can fix problem?

the hasnext() method going block until hit end of file marker on system.in because doesn't know if there's more input until reads full buffers worth or hits end of file (which can signal control-z on windows , control-d on unix). @ point system.in @ eof mark , there's no way re-open code.

if need process multiple streams of data system.in going have use sort of sentinel value (such word end) mark end of 1 input stream , beginning of another.


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 -