Results 1 to 4 of 4
Thread: problem with scanner
- 02-02-2013, 06:06 PM #1
Member
- Join Date
- Feb 2013
- Posts
- 2
- Rep Power
- 0
problem with scanner
hi people. i recently started with java and stuck on one thing.
i was doing something with scanner and noticed a problem
Scanner entry = new Scanner(System.in);
double stnm, ndnm, res;
System.out.println("Enter 1st number: ");
stnm = entry.nextDouble();
System.out.println("Enter 2nd number: ");
ndnm = entry.nextDouble();
res = stnm+ndnm;
System.out.println(res);
System.out.println("Hi. What's your name?");
String a = entry.nextLine();
System.out.println("Hi "+a);
why does scanner skip this line
i can enter first 2 integers but when i should be able to enter string it just skips that line.
why is it so? how can i fix that?
ty
- 02-02-2013, 07:13 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,377
- Blog Entries
- 7
- Rep Power
- 17
Re: problem with scanner
Before you want to read an entire line, you wanted to read a double; the nextDouble() method does just that and it doesn't read an end-of-line character that most likely was typed by you. The nextLine() method does see that end-of-line character and assumes that it had read an empty line. The remedy is simple: after the last nextDouble() call, call nextLine() to get rid of that empty line.
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
-
Re: problem with scanner
Try this:
String a = entry.next();
- 02-03-2013, 12:32 AM #4
Member
- Join Date
- Feb 2013
- Posts
- 2
- Rep Power
- 0
Similar Threads
-
max and min scanner problem
By jwl in forum New To JavaReplies: 2Last Post: 10-28-2012, 07:57 PM -
Problem with the Scanner
By Maretaga in forum New To JavaReplies: 6Last Post: 07-14-2011, 09:14 AM -
Scanner problem.
By keo in forum New To JavaReplies: 6Last Post: 04-07-2011, 08:30 AM -
Problem with scanner
By JavaJ in forum New To JavaReplies: 6Last Post: 03-16-2010, 08:51 PM -
Problem with scanner
By Kangaroo128 in forum New To JavaReplies: 11Last Post: 09-01-2009, 08:07 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks