View Single Post
  #1 (permalink)  
Old 01-23-2008, 04:00 AM
Kreuz14 Kreuz14 is offline
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.
Reply With Quote
Sponsored Links