• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

not sure what the problem is?

 
Chris Hathaway
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
im making a program that compares the dates and tells you how far apart they are timewise. im having a small problem at the moment thought...

here is part of my code:

------------------------------------------------------------------------------------------------------



--------------------------------------------------------------------------------
what it should do is

1. print "For the first date..."
then "enter month:" right under it

2. wait for user input

3. print "Enter year:"

4. wait for input

but on the second section of code, where it uses monthnumbertwo and yearnumbertwo, its displaying "Enter Year:" and "Enter Month:" at the same time. I have no idea what the problem is...
 
Henry Wong
author
Marshal
Pie
Posts: 21227
81
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Chris Hathaway wrote:im making a program that compares the dates and tells you how far apart they are timewise. im having a small problem at the moment thought...

here is part of my code:

------------------------------------------------------------------------------------------------------



--------------------------------------------------------------------------------
what it should do is

1. print "For the first date..."
then "enter month:" right under it

2. wait for user input

3. print "Enter year:"

4. wait for input

but on the second section of code, where it uses monthnumbertwo and yearnumbertwo, its displaying "Enter Year:" and "Enter Month:" at the same time. I have no idea what the problem is...



The issue is at line 5. Remember that the nextInt() method reads the next int -- unlike nextLine() it doesn't read the CR which ends the line. So, the nextInt() method reads the int, but leaves the CR character, which is immediate taken by the next nextLine() call. This is why you get a "Emter month:" immediately followed by "Enter Year:".

Henry

 
Chris Hathaway
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so then why does it work for the 1st section of code and not the second? Im having trouble getting what your saying lol
 
Willie Tsang
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
when line 4 is printed, system waits for you to type the year because your nextInt().
then you type " (year#)(enter/return)" , nextInt() will read the (year#).
On line 11, your nextLine() will read the (enter/return) and go to line 12.
 
Chris Hathaway
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I get the logic i guess, I still don`t see why i wouldnt have the same problem for the first section.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12149
31
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
on line three, the program pauses while the user types something. Let's say they type "March", which they follow with a press of the "enter" key.

The "scan.nextLine()" reads six characters...M-a-r-c-h-<enter key> There is nothing left in the keyboard buffer.

you print "Enter Year: "
scan.nextInt() pauses the program while the user types something. Let's say they type "1999" - again, followed by the "enter" key.

scan.nextInt() ONLY reads the digits - the '1', '9', '9', and the '9'. There is still an unread <enter> in the buffer, waiting for something to pick it up.

Lines 9 and 10 print some stuff out,

line 11 reads the next line. At this point, there is that un-read <enter> in the buffer, so it picks it up and lets the program move on to lines 12-13.
 
Chris Hathaway
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks to all whi helped! Figured it out, just removed Line from nextLine, works perfectly now
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic