# Problem with basic calculator - Beginner

• July 25th, 2013, 01:04 PM
Dream Hacked
Problem with basic calculator - Beginner
Hello community ^_^

I'm fairly new to the Java language and programming 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.

I appreciate all the help I can get. :)

Thanks

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; } }```
• July 25th, 2013, 09:18 PM
derekxec
Re: Problem with basic calculator - Beginner
well you have to make it loop back to the beginning you cant just add a comment and say //back to the beginning and expect the program to think yeap he wants to start over

the way you have it right now is if answer is yes do nothing
• July 25th, 2013, 09:31 PM
Dream Hacked
Re: Problem with basic calculator - Beginner
Quote:

Originally Posted by derekxec
well you have to make it loop back to the beginning you cant just add a comment and say //back to the beginning and expect the program to think yeap he wants to start over

the way you have it right now is if answer is yes do nothing

Lol, I didn't add the comment expecting the program to all of a sudden go back to the beginning.
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.
• July 26th, 2013, 12:00 AM
derekxec
Re: Problem with basic calculator - Beginner
lol sorry...you could make a boolean and set it to true and put everything in a while loop and set the value to false when the user puts in no

and you can use next instead of nextLine...also just remember the way you have it right now the Yes and No have to Yes and No not yes and no