Christophe
Christophe

Reputation: 147

Using user-inputted characters in If/Switch statements

I am taking my first Java class, and am having a hard time finishing this assignment.

The assignment itself is quite simple. All I have to do is prompt the user to input an integer, a character, then another integer.

If the character inputted is +, the two integers should be added together. If the character inputted is -, the two integers should be subtracted. Etc...

The problem is that I do not know how to accept a character from the user, then use it in my If/Switch statements.

Here are my prompts:

// Prompt the user to enter an integer
    System.out.print("Please enter an integer number:");
    int int1  = input.nextInt();

// Prompt the user to enter a character
// Since I could not use characters, I am using integers to represent them.
    System.out.print("Enter 0 for +, 1 for -, 2 for /, 3 for %, and 4 for *");
    int char1 = input.nextInt();

// Prompt the user to enter another integer
    System.out.print("Please enter another integer number:");
    int int2 = input.nextInt();

Here are my If/Switch statements: (I know they are redundant, but it's part of the assignment)

if (char1 == 0) {
    int ans = int1 + int2;
    System.out.println(int1 + " + " + int2 + " = " + ans);
} else if (char1 == 1) {
    int ans = int1 - int2;
    System.out.println(int1 + " - " + int2 + " = " + ans);
} else if (char1 == 2) {
    int ans = int1 / int2;
    System.out.println(int1 + " / " + int2 + " = " + ans);
} else if (char1 == 3) {
    int ans = int1 % int2;
    System.out.println(int1 + " % " + int2 + " = " + ans);
} else if (char1 == 4) {
    int ans = int1 * int2;
    System.out.println(int1 + " * " + int2 + " = " + ans);
} else
    System.out.println("Invalid operator");

int result = 0;
switch (char1) {
    case 0: result = int1 + int2;
            System.out.println(int1 + " + " + int2 + " = " + result); break;
    case 1: result = int1 - int2;
            System.out.println(int1 + " - " + int2 + " = " + result); break;
    case 2: result = int1 / int2;
            System.out.println(int1 + " / " + int2 + " = " + result); break;
    case 3: result = int1 % int2;
            System.out.println(int1 + " % " + int2 + " = " + result); break;
    case 4: result = int1 * int2;
            System.out.println(int1 + " * " + int2 + " = " + result); break;
    default: System.out.println("Invalid operator");
}

Everything else works just fine. I'm just trying to change from using 0 for +, 1 for -, to just using +, -, /, %, and *.

Upvotes: 1

Views: 3583

Answers (3)

MrSmith42
MrSmith42

Reputation: 10151

You can use a char from the user input this way:

A char is only an unsigned integer value 0..65535 so you can write e.g. + for the value 43 [Which is the unicode of '+']

switch (char1) {
    case '+': result = int1 + int2;
        System.out.println(int1 + " + " + int2 + " = " + result); break;
    case '-': result = int1 - int2;
        System.out.println(int1 + " - " + int2 + " = " + result); break;
    case '/': result = int1 / int2;
        System.out.println(int1 + " / " + int2 + " = " + result); break;
    case '%': result = int1 % int2;
        System.out.println(int1 + " % " + int2 + " = " + result); break;
    case '*': result = int1 * int2;
        System.out.println(int1 + " * " + int2 + " = " + result); break;
    default: System.out.println("Invalid operator");
}

Upvotes: 0

Alya'a Gamal
Alya'a Gamal

Reputation: 5638

you can take the operator from the user as String or Char

Like this :

 Scanner input = new Scanner(System.in);
        // Prompt the user to enter an integer
        System.out.print("Please enter an integer number:");
        int int1 = input.nextInt();

// Prompt the user to enter a character
// Since I could not use characters, I am using integers to represent them.
        System.out.print("Enter +, -,  /,  %, *");
        char char1 = input.next().charAt(0);

// Prompt the user to enter another integer
        System.out.print("Please enter another integer number:");
        int int2 = input.nextInt();

        if (char1=='+') {
            int ans = int1 + int2;
            System.out.println(int1 + " + " + int2 + " = " + ans);
        } else if (char1=='-') {
            int ans = int1 - int2;
            System.out.println(int1 + " - " + int2 + " = " + ans);
        } else if (char1=='/') {
            int ans = int1 / int2;
            System.out.println(int1 + " / " + int2 + " = " + ans);
        } else if (char1=='%') {
            int ans = int1 % int2;
            System.out.println(int1 + " % " + int2 + " = " + ans);
        } else if (char1=='*') {
            int ans = int1 * int2;
            System.out.println(int1 + " * " + int2 + " = " + ans);
        } else {
            System.out.println("Invalid operator");
        }

        int result = 0;

        switch (char1) {
            case '+':
                result = int1 + int2;
                System.out.println(int1 + " + " + int2 + " = " + result);
                break;
            case '-':
                result = int1 - int2;
                System.out.println(int1 + " - " + int2 + " = " + result);
                break;
            case '/':
                result = int1 / int2;
                System.out.println(int1 + " / " + int2 + " = " + result);
                break;
            case '%':
                result = int1 % int2;
                System.out.println(int1 + " % " + int2 + " = " + result);
                break;
            case '*':
                result = int1 * int2;
                System.out.println(int1 + " * " + int2 + " = " + result);
                break;
            default:
                System.out.println("Invalid operator");
        }

Upvotes: 2

tckmn
tckmn

Reputation: 59303

You can do this:

System.out.print("Enter +, -, /, or *");
char char1 = input.nextLine().charAt(0);

nextLine will get the entire thing that the user input, and charAt gets the character at the specified index.

Therefore, charAt(0) gets the first character. Here is a visual explanation: the top is the string and the bottom is what character number it is:

|t|h|i|s| |i|s| |a| |t|e|s|t|
-----------------------------
|0|1|2|3|4|5|6|7|8|9|1|1|1|1|
| | | | | | | | | | |0|1|2|3|

Upvotes: 1

Related Questions