java - My program is terminated without scanning string -


when execute code program terminated without scanning string.

double x, y;     string s;     scanner scan = new scanner(system.in);     system.out.println("enter number: ");     x = scan.nextdouble();     system.out.println("enter number 2: ");     y = scan.nextdouble();     system.out.println("enter operater: x,+,/,-");     s = scan.nextline();     if(s.equals("x"))     {         system.out.print(x * y);      }     else if(s.equals("+"))     {         system.out.print(x + y);      }     else if(s.equals("/"))     {         system.out.print(x / y);      }     else if(s.equals("-"))     {         system.out.print(x - y);      }     scan.close(); 

my program ends before s = scan.nextline(); how come ends before?

end of line leave in buffer. next( ) reads token buffer until next white space, while nextline( ) reads \n

... system.out.print("enter number 2: "); y = scan.nextdouble(); system.out.print("enter operater: x,+,/,-"); s = scan.next(); ...  enter number: 1 enter number 2: 2 enter operater: x,+,/,-- -1.0 

Comments

Popular posts from this blog

javascript - DIV "hiding" when changing dropdown value -

Does Firefox offer AppleScript support to get URL of windows? -

android - How to install packaged app on Firefox for mobile? -