problem with Java's Scanner: InputMismatchException

Vitangelo Moscarda :

I have this code:

import java.util.Scanner;

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

        int num1, num2, num3;
        int max;

        System.out.println("Inserisci il primo numero: ");
        num1 = scanner.nextInt();

        System.out.println("Inserisci il secondo numero: ");
        num2 = scanner.nextInt();

        System.out.println("Inserisci il terzo numero: ");
        num3 = scanner.nextInt();

        if (num1 > num2 && num1 > num3) {
            max = num1;
        } else if (num2 > num1 && num2 > num3) {
            max = num2;
        } else {
            max = num3;
        }

        System.out.println("Il maggiora trai tre è: " + max);
        scanner.close();
    }
}

When I run it, before I can input the first number the console gives me this error:

Inserisci il primo numero:
Exception in thread "main" java.util.InputMismatchException
    at java.base/java.util.Scanner.throwFor(Scanner.java:939)
    at java.base/java.util.Scanner.next(Scanner.java:1594)
    at java.base/java.util.Scanner.nextInt(Scanner.java:2258)
    at java.base/java.util.Scanner.nextInt(Scanner.java:2212)
    at Maggiore3Valori.main(Maggiore3Valori.java:13)

I read around that the problem is with the type of variables; but they should be right. What does this error mean? How can I solve it?

Andy Turner :
Scanner scanner = new Scanner ("System.in");

This creates a scanner that reads a file in the current directory named System.in.

That's probably not what you mean. To read from the standard input, remove the quotes:

Scanner scanner = new Scanner (System.in);

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=415588&siteId=1