user3546834
user3546834

Reputation: 23

Java Calculator errors java scanner

I'm trying to make a simple calculator and need help with somthing

import java.util.*;
import javax.swing.*;

public class HiWorld {
    public static void main(String[] args) {
        System.out.println("Type 2 Numbers");
        Scanner input1 = new Scanner(System.in);
        Scanner input2 = new Scanner(System.in);
        answer = input1 + input2;
        System.out.println(answer);
    }

It says that the operator + is undefined for the argument types java.util.scanner.

Upvotes: 2

Views: 122

Answers (2)

Misa Lazovic
Misa Lazovic

Reputation: 2823

What you are doing there is creating two objects that read from standard input (your keyboard) and then, you are trying to add those objects together.

For start, use:

import java.util.*;

public class HiWorld {
    public static void main(String[] args) {
        System.out.println("Type 2 Numbers");
        Scanner input = new Scanner(System.in);
        int x = input.nextInt();
        int y = input.nextInt();
        int answer = x + y;
        System.out.println(answer);
    }
}

Upvotes: 1

Andres
Andres

Reputation: 10717

In Java you can only use the + operator for adding two numbers or concatenating two Strings. Not for doing anything with scanners.

Upvotes: 1

Related Questions