0

Sorry for the noob question. This is my first day trying out Java programming. I need help retrieving the users input of which operator the user wants to use with integers.

import java.util.Scanner;

public class calculator {

    public static void main (String args[]) {

        Scanner number = new Scanner(System.in);
        double num1,num2,answer;
        char operator;

        System.out.println("Enter first number: ");
        num1 = number.nextDouble();

        operator = number.nextChar(); /* the "nextChar" is not correct */

        System.out.println("Enter second number: ");
        num2 = number.nextDouble();

        if (operator = '+'){
            answer = num1 + num2; /* If statements do not work like this */
        }                             /* I would use "else if", unsure if it allows */
        if (operator = '-'){
            answer = num1 - num2;
        }
        if (operator = '*'){
            answer = num1 - num2;
        }
        if (operator = '/'){
                answer = num1 / num2;
        }

        System.out.println(num1 + num2 + operator + answer);

    }
}

Edited Version:

import java.util.Scanner;

public class calculator{

    public static void main (String args[]) {

        Scanner userInput = new Scanner(System.in);

        String operator;
        double num1,num2,answer = 0;



        System.out.println("Enter first number: ");
        num1 = userInput.nextDouble();

        System.out.println("Enter operator: ");
        operator = userInput.next();


        System.out.println("Enter second number: ");
        num2 = userInput.nextDouble();

        if (operator.equals ("+")){
            answer = num1 + num2;
        }
        else if (operator.equals ("-")){
            answer = num1 - num2;
        }
        else if (operator.equals ("*")){
            answer = num1 * num2;
        }
        else if (operator.equals ("/")){
                answer = num1 / num2;
        }

        System.out.println("First number:" + num1);
        System.out.println("Operator:" + operator);
        System.out.println("Second number:" + num2);

        System.out.println("Answer: " + answer);
    }
}

First number:10.0

Operator:*

Second number:3.14

Answer: 31.400000000000002

Thanks a lot guys! Helped a lot!

greuze
  • 3,778
  • 4
  • 38
  • 60
Cakes
  • 19
  • 1
  • 6

4 Answers4

0

Use == in comparisons, not = (that means assignation).

greuze
  • 3,778
  • 4
  • 38
  • 60
0

As other have already pointed out, use the == operator to check for equality. The = operator will assign values instead.

In order to read a single character from the Scanner, take a look at the following question: Scanner method to get a char. There you'll find the suggestion to use Scanner.findInLine() method, which receives a regular expression as an argument. Providing the . wildcard character as pattern, you'll be able to get a String with the one next character, and using charAt(0) on it, you'll get it as a char.

Scanner sc = new Scanner("abc");
char ch = sc.findInLine(".").charAt(0);
System.out.println(ch); // prints "a"

Of course, you could also just use Scanner.next() to get the next word as a String, and just get its first character. But take into account that doing so would discard from the input buffer the rest of the word.

Community
  • 1
  • 1
Xavi López
  • 26,413
  • 11
  • 94
  • 146
0

Ok. This works and can help you on your road to java

public class Tester4 {

    public static void main(String args[]) {

        Scanner input = new Scanner(System.in);
        double num1, num2, answer = 0;
        String operator;

        System.out.println("Enter first number: ");
        num1 = input.nextDouble();

        System.out.println("Enter operator: ");
        operator = input.next();

        System.out.println("Enter second number: ");
        num2 = input.nextDouble();

        if(operator.equals("+")) {
            answer = num1 + num2;
        } else if(operator.equals("-")) {
            answer = num1 - num2;
        } else if(operator.equals("*")) {
            answer = num1 - num2;
        } else if(operator.equals("/")) {
            answer = num1 / num2;
        }

        System.out.println("First number:" + num1);
        System.out.println("Operator:" + operator);
        System.out.println("Second number:" + num2);

        System.out.println("Answer: " + answer);
    }

}
Festus Tamakloe
  • 11,163
  • 9
  • 49
  • 64
  • Awesome! So what you did is change the == comparison sign into "equals" which means the same thing? Then you put the operator in quotes so it can be read as a string? Also, you replaced char with string. Perfect :) – Cakes Jan 31 '13 at 11:23
  • In java there are some syntax rules. if you want to compare 2 string it recommended to use equals instead of == you can use == if you want to compare e.g. 2 int. The scanner class doesn't support char directly so you use string at the place. if you like my answer just vote up and accept it – Festus Tamakloe Jan 31 '13 at 11:36
  • Ahh, I see, I understand. Okay, I didn't know about the vote and accepting (just started using stackoverflow today). I'll vote once I can since I need 15 reputation to do so lol. Thanks, once again. – Cakes Jan 31 '13 at 11:45
-1

You need to use

if (operator == '+') 

Notice the 'double equal to'.

mtk
  • 11,504
  • 15
  • 67
  • 104