-1

yeah i tried to make a really simple calculator myself in java. but it cannot make print the math out i type. it has no error i tried to search the mistake but cant find a solution.

import java.util.Scanner;

public class Øvelse {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);

        String calculator;

        System.out.print("skriv nummer: ");
        int num1 = in.nextInt();

        System.out.print("skriv nummer: ");
        int num2 = in.nextInt();

        Scanner op = new Scanner(System.in);

        System.out.print("skriv + - * /: ");
        calculator = op.nextLine();

        if(calculator == "+") {
            System.out.println("resultat1 " + num1 + num2);
        }

        if(calculator == "-") {
            System.out.println("resultat3 ");
        }

        if(calculator == "/") {
            System.out.println("resultat3 " + num1 / num2);
        }

        if(calculator == "*") {
            System.out.println("resultat4 " + num1 * num2);
        }
    }
}
dat3450
  • 954
  • 3
  • 12
  • 25
fozzi215
  • 7
  • 2
  • 3
    It's partly a dupe of the compare strings question, and partly of the nextFoo question. – Dawood ibn Kareem May 29 '17 at 00:09
  • 2
    fozzi - Read [this](https://stackoverflow.com/q/13102045) and [this](https://stackoverflow.com/q/513832). They describe the two things you're doing wrong. – Dawood ibn Kareem May 29 '17 at 00:11
  • if(calculator.equals("+"){ Use .equals not == Also use if and else if statements or better yet, a switch statement. –  May 29 '17 at 00:34
  • There are two issues, 1) he's using integer comparison instead of string and 2) he's appending numbers rather than doing math, see my answer below. – JordanGS May 29 '17 at 00:46
  • 1
    Tip: [Use a debugger](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) – OneCricketeer May 29 '17 at 00:46
  • @JordanGS Three issues. See my earlier comment for the other one. – Dawood ibn Kareem May 29 '17 at 01:12
  • @DawoodibnKareem the scanner isn't skipping anything so there's no scanner issue. – JordanGS May 29 '17 at 01:43
  • 1
    I disagree completely. Try it. You'll see. – Dawood ibn Kareem May 29 '17 at 01:51
  • @DawoodibnKareem i don't care if you disagree, there's no reason for the scanner to miss anything. There's nothing wrong with the code, but i did humor you and tried it out. The scanner doesn't have any issues. It takes 2 numbers and an operator. Maybe you need to try it yourself? Maybe it was an issue in JDK6 or early versions of JDK7, don't know. I'm running JDK 8 and the scanner works fine. Try it yourself if you don't believe me, – JordanGS May 29 '17 at 03:15

2 Answers2

0

Couple issues

1) When doing string compare. You can't use ==, to use string comparison then you should as an example. calculator.equals("+"). The intA == intB is when comparing integers, varA.equals(varB) is when you compare strings.

2) You can do math like that

System.out.println("resultat1 " + num1 + num2); will just append text, you have to isolate the math with brackets (..), such as

System.out.println("resultat1 " + (num1 + num2));, this will now do addition.

JordanGS
  • 2,894
  • 5
  • 13
  • 21
0

Firstly, to compare Strings you needa use equals not the == operator (in your case you should even use a switch statement rather than multiple if statements).

Secondly "abc"+3+4 is different from "abc"+(3+4).

Vivick
  • 2,051
  • 2
  • 6
  • 19