Results 1 to 5 of 5
- 01-23-2008, 02:00 AM #1
Member
- Join Date
- Jan 2008
- Posts
- 5
- Rep Power
- 0
Peculiarty in code of simple program...
Why is: name = input.nextLine(); necessary?Java 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 + "."); } }
I wrote the program but I commented to just make the problem clear in the code.Last edited by Kreuz14; 01-23-2008 at 02:24 AM.
- 01-23-2008, 02:33 AM #2
Member
- Join Date
- Jan 2008
- Posts
- 5
- Rep Power
- 0
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 :PLast edited by Kreuz14; 01-23-2008 at 02:37 AM.
- 01-23-2008, 02:47 AM #3
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:
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.Java Code:System.out.println("Enter three integers on one line.");
Cheers :)Vote for the new slogan to our beloved Java Forums! (closes on September 4, 2008)
Want to voice your opinion on your IDE/Editor of choice? Vote now!
Got a little Capt'n in you? (drink responsibly)
- 01-23-2008, 02:52 AM #4
Member
- Join Date
- Jan 2008
- Posts
- 5
- Rep Power
- 0
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 02:57 AM.
- 01-23-2008, 03:27 AM #5
Please see here for your second question, it becomes pretty evident by reading the specs.
Last edited by CaptainMorgan; 01-23-2008 at 03:29 AM.
Vote for the new slogan to our beloved Java Forums! (closes on September 4, 2008)
Want to voice your opinion on your IDE/Editor of choice? Vote now!
Got a little Capt'n in you? (drink responsibly)
Similar Threads
-
simple problem - code wont compile
By dirtycash in forum New To JavaReplies: 1Last Post: 11-20-2007, 05:49 PM -
simple code
By elizabeth in forum New To JavaReplies: 1Last Post: 08-07-2007, 06:49 PM -
help with simple program in java
By katie in forum New To JavaReplies: 2Last Post: 08-06-2007, 08:03 PM -
problem with a simple java code
By boy22 in forum New To JavaReplies: 2Last Post: 08-03-2007, 02:46 AM -
help with simple java program
By leonard in forum New To JavaReplies: 3Last Post: 07-30-2007, 09:40 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks