Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 01-23-2008, 04:00 AM
Member
 
Join Date: Jan 2008
Posts: 5
Kreuz14 is on a distinguished road
Peculiarty in code of simple program...
Code:
import java.util.*; public class SimpleProgram { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.println("Enter three integers on one line."); int num = input.nextInt(); num += input.nextInt(); num += input.nextInt(); System.out.println("Enter your name on a new line."); String name = input.nextLine(); // Program produces logic error if this line does not use the nextLine method (ie. String name;) in conjunction with the next line. name = input.nextLine(); // Necessary to avoid an odd logic error, previous nextLine method does not 'appear' to occur during runtime. System.out.println(name + " owes Steven $" + num + "."); } }
Why is: name = input.nextLine(); necessary?
I wrote the program but I commented to just make the problem clear in the code.

Last edited by Kreuz14 : 01-23-2008 at 04:24 AM.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 01-23-2008, 04:33 AM
Member
 
Join Date: Jan 2008
Posts: 5
Kreuz14 is on a distinguished road
I'm currently running this on DrJava. I havn't tried it on Eclipse since its such a simple program but should the IDE matter since the compiler is from the JDK right?

Edit: Odd then... this is just a simple prog i need to submit as an assignment lol and I can't believe im spending more than a few minutes on it. Perhaps i'll just send it as is and leave the odd lines of code commented.

EDIT2: Er, I'm not talking to myself. Someone deleted their previous post :P

Last edited by Kreuz14 : 01-23-2008 at 04:37 AM.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 01-23-2008, 04:47 AM
CaptainMorgan's Avatar
Moderator
 
Join Date: Dec 2007
Location: NewEngland, US
Posts: 841
CaptainMorgan will become famous soon enoughCaptainMorgan will become famous soon enough
Send a message via AIM to CaptainMorgan
No worries Kreuz... I was mistaken in my previous thought and I was hoping to delete it swiftly.. I didn't think you'd be so quick!

Anyways, your intent shown by this line:
Code:
System.out.println("Enter three integers on one line.");
indicates you want three integers on one line.. pretty obvious. However, nextInt() keeps reading and doesn't put a '\n' char on the buffer. Review Scanner for more details.

Cheers
__________________

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
to our beloved Java Forums!
(closes on September 4, 2008)
Want to voice your opinion on your IDE/Editor of choice?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
!
Got a little Capt'n in you? (drink responsibly)
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 01-23-2008, 04:52 AM
Member
 
Join Date: Jan 2008
Posts: 5
Kreuz14 is on a distinguished road
ah, thanks! Tough problem to catch, is there an elegant solution to this or is my current code fine if I add a comment to indicate the reason for it?

Also, what exactly is happening to the first nextLine() method call? Does nextInt() just take it as its input?

Last edited by Kreuz14 : 01-23-2008 at 04:57 AM.
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 01-23-2008, 05:27 AM
CaptainMorgan's Avatar
Moderator
 
Join Date: Dec 2007
Location: NewEngland, US
Posts: 841
CaptainMorgan will become famous soon enoughCaptainMorgan will become famous soon enough
Send a message via AIM to CaptainMorgan
Please see here for your second question, it becomes pretty evident by reading the specs.
__________________

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
to our beloved Java Forums!
(closes on September 4, 2008)
Want to voice your opinion on your IDE/Editor of choice?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
!
Got a little Capt'n in you? (drink responsibly)

Last edited by CaptainMorgan : 01-23-2008 at 05:29 AM.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
simple problem - code wont compile dirtycash New To Java 1 11-20-2007 07:49 PM
simple code elizabeth New To Java 1 08-07-2007 08:49 PM
help with simple program in java katie New To Java 2 08-06-2007 10:03 PM
problem with a simple java code boy22 New To Java 2 08-03-2007 04:46 AM
help with simple java program leonard New To Java 3 07-30-2007 11:40 AM


All times are GMT +3. The time now is 02:20 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org