0

what is the proplem it should ask for number 1 and number2 and operater

package com.company;

import java.util.Scanner;


public class Main {

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        System.out.print("Enter number 1: ");
        int operand1 = scan.nextInt();
        System.out.print("Enter operator: ");
        String operator = new Scanner(System.in).nextLine();

        System.out.print("Enter number 2: ");
        int operand2 =  scan.nextInt();
        if (operator == "+") {
            return int.class(operand1 + operand2);
        }
        else if (operator == "-") {
            return int.class(operand1 - operand2);
        }
        else if (operator == "*") {
            return int.class(operand1 * operand2);
        }
        else {
            return "error...";
        }
    }

}
// Error:(18, 29) java: ';' expected
// Error:(18, 39) java: not a statement

errors Error:(18, 29) java: ';' expected + Error:(18, 39) java: not a statement

  • Where did you get `int.class(...)` from? It is not valid Java. – khelwood Jul 12 '20 at 15:45
  • what to do ..... – marwan azzam Jul 12 '20 at 15:46
  • Get rid of all the `int.class` bits. Compare strings with `.equals` instead of `==`. Don't create multiple scanners. Also see [Scanner is skipping nextLine() after using next() or nextFoo()?](https://stackoverflow.com/questions/13102045/scanner-is-skipping-nextline-after-using-next-or-nextfoo). Also your `void main` function can't return anything. It's void. – khelwood Jul 12 '20 at 15:47
  • You are using return in a function, which has no return value (`void`). – Strubbl Jul 12 '20 at 15:49
  • Is more useful to mark on code where exactly is the error, instead of tely us `Error:(18, 29)`. – KunLun Jul 12 '20 at 15:49

1 Answers1

0

There are a few issues:

  1. main is declared as void so you shouldn't return anything
  2. string comparison is done with equals in Java, not ==
  3. you probably want to display the result rather than returning it anyways: use System.out.print instead of return!
  4. as mentioned above: I'm not sure what you wanted to do by wrapping the arithmetic operations with int.class(...) but it's not doing what you think it's doing.

See corrections below (or run directly here):

import java.util.Scanner;   

public class Main {

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        System.out.print("Enter number 1: ");
        int operand1 = scan.nextInt();
        System.out.print("Enter operator: ");
        String operator = new Scanner(System.in).nextLine();

        System.out.print("Enter number 2: ");
        int operand2 =  scan.nextInt();
        if (operator.equals("+")) {
            System.out.print(operand1 + operand2);
        } else if (operator.equals("-")) {
            System.out.print(operand1 - operand2);
        } else if (operator.equals("*")) {
            System.out.print(operand1 * operand2);
        } else {
            System.out.print("error: unknown Operation: " + operator);
        }
    }

}
Nir Alfasi
  • 49,889
  • 11
  • 75
  • 119