Results 1 to 5 of 5
  1. #1
    Dream Hacked is offline Member
    Join Date
    Jul 2013
    Posts
    4
    Rep Power
    0

    Default Problem with a basic calculator program - Beginner

    Hello community ^_^

    I'm fairly new to the Java language, programming, and this community so bare with me on this one. I'm teaching myself and I'm using every resource I can to get better.
    I'm currently learning about methods and I got a little creative.
    I've created a simple calculator program implementing the use of methods (I'm not sure whether this is the correct way, but I'm doing it anyway)

    -----------------------------------------------------

    There are two problems I'm having with the code:

    1. It's not letting the user input Yes or No when the it gets to "input = nextLine();". Instead, it skips over this and ends the program.

    2. I'm not sure what to put in the if statement or in general to have the code loop back to the beginning of the program and start over instead of having to run the program over again.

    Java Code:
        static int number;
        static String passing, input;
        static int numOne, numTwo, numThree;
     
        static Scanner sc = new Scanner(System.in);
     
        public static void main(String[] args) {
     
            System.out.println("Input first number");
            numOne = sc.nextInt();
     
            System.out.println("Input second number");
            numTwo = sc.nextInt();
     
            System.out.println("What would you like to do with these numbers?");
            System.out.println("1. Addition "
                    + "2. Subtraction "
                    + "3. Multiplication "
                    + "4. Division");
            System.out.print(" Enter number: ");
     
             anotherOption();
     
             System.out.println(Recieving());
             System.out.println("Yes/No");
     
              input = sc.nextLine();
     
             if(input.equals("Yes")){
              //  back to the beginning
             }
     
             else if(input.equals("No")){
                 System.exit(0);
             }
     
        }
     
        public static void anotherOption() {
     
            number = sc.nextInt();
     
            switch(number) {
                case 1: 
                    System.out.println(numOne + numTwo);
                    break;
                case 2:
                    System.out.println(numOne - numTwo);
                    break;
                case 3:
                    System.out.println(numOne * numTwo);
                    break;
                case 4:
                    System.out.println(numOne / numTwo);
                    break;
                default:
                    System.out.println("Invalid input");
                    break;
     
     
            }
     
            passing = "Would you like to do anything else?";
     
        }
     
        public static String Recieving(){
            return passing;
        }
    }
    Note: I posted this on another forum and got no help. The guy told me that I was dumb for expecting a comment to have the program take me back to the beginning. I didn't add the comment expecting the program to all of a sudden go back to the beginning. I'm new, but I'm not an idiot.
    I added it so I would know what I code to replace the comment with.

    Another reason I added the comment is because the IDE will not record the user's input into "input = sc.nextLine;".
    I wanted to know why my code is doing that. After that, I wanted to know what code is appropriate to fill in for the comment.


    I appreciate all the help I can get.

    Thanks
    Last edited by Dream Hacked; 07-26-2013 at 04:08 AM.

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    2,923
    Rep Power
    4

    Default Re: Problem with a basic calculator program - Beginner

    When you do an nextInt(), it still leaves the rest of the line in the input buffer (you can have two ints on the same line). After the second nextInt() there is still a line terminator character there. So you need to immediately do a nextLine() to clear it out.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    Dream Hacked is offline Member
    Join Date
    Jul 2013
    Posts
    4
    Rep Power
    0

    Default Re: Problem with a basic calculator program - Beginner

    Quote Originally Posted by jim829 View Post
    When you do an nextInt(), it still leaves the rest of the line in the input buffer (you can have two ints on the same line). After the second nextInt() there is still a line terminator character there. So you need to immediately do a nextLine() to clear it out.

    Regards,
    Jim
    Thanks for replying about the problem, Jim.

    I'm having a little trouble understanding what you said about the nextLine() having to clear out the terminator character after the nextInt().

    Could you give an example of this?


    Thanks

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

    Default Re: Problem with a basic calculator program - Beginner

    It's fairly straightforward:
    -- the System.out.println(...) statement puts a line of text on the screen, telling the user what input is expected (a number).
    -- the user types numeric characters and presses <Enter>. This String, endingg with a newline character, goes into the Scanner's buffer.
    -- Scanner#nextInt() consumes the numeric characters and converts them to an int value.
    -- the Scanner buffer now holds the newline character
    -- you need to consume that newline with a call to nextLine() (which consumes all characters up to and including a newline) before doing anything else with the Scanner.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  5. #5
    Dream Hacked is offline Member
    Join Date
    Jul 2013
    Posts
    4
    Rep Power
    0

    Default Re: Problem with a basic calculator program - Beginner

    Quote Originally Posted by DarrylBurke View Post
    It's fairly straightforward:
    -- the System.out.println(...) statement puts a line of text on the screen, telling the user what input is expected (a number).
    -- the user types numeric characters and presses <Enter>. This String, endingg with a newline character, goes into the Scanner's buffer.
    -- Scanner#nextInt() consumes the numeric characters and converts them to an int value.
    -- the Scanner buffer now holds the newline character
    -- you need to consume that newline with a call to nextLine() (which consumes all characters up to and including a newline) before doing anything else with the Scanner.

    db
    Thank you! It took me a while, but I understand it like this: I used nextLine(); for 2 reasons: First, to capture the String the user enters (using the method's return value). And second, to advance to the next line. nextint(); like you said, holds the newline character. I added sc.newLine(); under the call for anotherOption(); method and it worked.

    I also put the a do while loop in the program and now the whole program repeats.

    Problem solved.

    Best regards,

    Dream Hacked

Similar Threads

  1. Calculator beginner tutorial(s)
    By Mr.Greedy in forum New To Java
    Replies: 4
    Last Post: 06-14-2012, 05:08 AM
  2. Trying to do a basic calculator...
    By TheChodja in forum New To Java
    Replies: 1
    Last Post: 04-08-2012, 09:42 PM
  3. Problem with if statement in a calculator program
    By peterhabe in forum New To Java
    Replies: 2
    Last Post: 08-26-2011, 05:07 PM
  4. Basic Java Calculator Code Problem
    By SerbianSergeant in forum New To Java
    Replies: 7
    Last Post: 07-22-2011, 01:50 PM
  5. Basic Calculator
    By trtoy in forum New To Java
    Replies: 1
    Last Post: 12-25-2009, 01:06 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
  •