# Problem with a basic calculator program - Beginner

• 07-26-2013, 04:06 AM
Dream Hacked
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.

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
• 07-26-2013, 04:42 AM
jim829
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
• 07-26-2013, 05:28 AM
Dream Hacked
Re: Problem with a basic calculator program - Beginner
Quote:

Originally Posted by jim829
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

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
• 07-26-2013, 05:56 AM
DarrylBurke
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
• 07-26-2013, 08:54 AM
Dream Hacked
Re: Problem with a basic calculator program - Beginner
Quote:

Originally Posted by DarrylBurke
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