This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes not sure what the problem is? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "not sure what the problem is?" Watch "not sure what the problem is?" New topic
Author

not sure what the problem is?

Chris Hathaway
Greenhorn

Joined: Feb 08, 2012
Posts: 16
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
Sheriff

Joined: Sep 28, 2004
Posts: 18914
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Chris Hathaway
Greenhorn

Joined: Feb 08, 2012
Posts: 16
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

Joined: Nov 20, 2011
Posts: 24
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

Joined: Feb 08, 2012
Posts: 16
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

Joined: Oct 02, 2003
Posts: 11441
    
  16

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.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Chris Hathaway
Greenhorn

Joined: Feb 08, 2012
Posts: 16
Thanks to all whi helped! Figured it out, just removed Line from nextLine, works perfectly now
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: not sure what the problem is?