Results 1 to 5 of 5
  1. #1
    Kreuz14 is offline Member
    Join Date
    Jan 2008
    Posts
    5
    Rep Power
    0

    Default Peculiarty in code of simple program...

    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 + ".");
      }
    }
    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 03:24 AM.

  2. #2
    Kreuz14 is offline Member
    Join Date
    Jan 2008
    Posts
    5
    Rep Power
    0

    Default

    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 03:37 AM.

  3. #3
    CaptainMorgan's Avatar
    CaptainMorgan is offline Moderator
    Join Date
    Dec 2007
    Location
    NewEngland, US
    Posts
    835
    Rep Power
    10

    Default

    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:
    Java 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 :)
    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)

  4. #4
    Kreuz14 is offline Member
    Join Date
    Jan 2008
    Posts
    5
    Rep Power
    0

    Default

    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 03:57 AM.

  5. #5
    CaptainMorgan's Avatar
    CaptainMorgan is offline Moderator
    Join Date
    Dec 2007
    Location
    NewEngland, US
    Posts
    835
    Rep Power
    10

    Default

    Please see here for your second question, it becomes pretty evident by reading the specs.
    Last edited by CaptainMorgan; 01-23-2008 at 04: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

  1. simple problem - code wont compile
    By dirtycash in forum New To Java
    Replies: 1
    Last Post: 11-20-2007, 06:49 PM
  2. simple code
    By elizabeth in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 06:49 PM
  3. help with simple program in java
    By katie in forum New To Java
    Replies: 2
    Last Post: 08-06-2007, 08:03 PM
  4. problem with a simple java code
    By boy22 in forum New To Java
    Replies: 2
    Last Post: 08-03-2007, 02:46 AM
  5. help with simple java program
    By leonard in forum New To Java
    Replies: 3
    Last Post: 07-30-2007, 09:40 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •