0

I looked at some other similar posts but was not able to figure out the solution, hence looking for your valuable input, thanks in advance.

What I want: You will be asked to enter a sign (+ , -, /) and the output will display "You entered minus" (if your input was -)

Class that takes the input

import java.util.Scanner;

public class TakeSign {
    Scanner userInput = new Scanner(System.in); /*Create an object of scanner class*/

    public char mySign() { 
        System.out.print("Enter a sign: ");
        char input2 = userInput.next().charAt(0);
        return input2;
    }
}

Main Class

public class Main {
    public static void main (String[]args) {
        TakeSign ts = new TakeSign();
        if (ts.mySign() == '+') {
            System.out.println("You entered plus");
        }
        else if (ts.mySign() == '-') {
            System.out.println("You entered minus");
        }
        else if (ts.mySign() == '/') {
            System.out.println("You entered division");
        }
    }
}

Problem

If my 1st input is / (division) I get asked 3 times. I was expecting to be asked just 1 time.

Enter a sign: /
Enter a sign: /
Enter a sign: /
You entered division

I think the problem is in the loop which I probably did not write correctly. Can you please point me to right direction?

4 Answers 4

4

You need to assign ts.mySign() to a variable and use that variable in your if statements

public static void main(String[] args) {
    TakeSign ts = new TakeSign();
    char sign = ts.mySign();
    if (sign == '+') {
        System.out.println("You entered plus");
    } else if (sign == '-') {
        System.out.println("You entered minus");
    } else if (sign == '/') {
        System.out.println("You entered division");
    }
}
Sign up to request clarification or add additional context in comments.

Comments

2

The mySign method asks the user for input. So if you call ts.mySign() three times, it will ask the user three times. But this is what you're doing in your chain of if and else statements.

You need to call ts.mySign() just once before the if statements, assign the result to a variable, then just check the value of that variable in each of the if statements.

1 Comment

Thank You that explains the problem.
1

You might use

 switch( ts.mySign() ){
 case '+':
     System.out.println ("You entered plus");
     break;
 case '-':
     System.out.println ("You entered mimus");
     break;
 case '*':
     System.out.println ("You entered asterisk");
     break;
 case '/':
     System.out.println ("You entered slash");
     break;
 default:
     System.out.println ("Input error");
     break;
}

Comments

0

You're calling ts.mySign() 3 times in your if-statement.

Call it once and store it in a variable and then compare against your signs.

i.e.

char sign = ts.mySign();
if (sign == '+')
...

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.