Varuzhan Stepanyan
Varuzhan Stepanyan

Reputation: 97

Error in java console program

import java.util.Scanner;

public class Example {
    public static void main(String Args[]) {
        Scanner sc = new Scanner("System.in");
        System.out.println("Yntreq (1/2):");
        int y = sc.nextInt();
        switch (y) {
            case 1:
                System.out.println("Duq yntrel eq 1-y");
                break;
            case 2:
                System.out.println("Duq yntrel eq 2-y");
                break;
            default:
                break;
        }

    }
}

And when it runing,eclipse show this error

Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at Example.main(Example.java:7)

Upvotes: 3

Views: 156

Answers (4)

Girdhar Singh Rathore
Girdhar Singh Rathore

Reputation: 5615

you have passed invalid parameter in constructor, constructor syntax for input stream is -

public Scanner(InputStream source)

Constructs a new Scanner that produces values scanned from the specified input stream. Bytes from the stream are converted into characters using the underlying platform's default charset. Parameters: source - An input stream to be scanned

example : Scanner sc = new Scanner(System.in);

Upvotes: 0

Rafiq
Rafiq

Reputation: 750

Remove double quote" " from "System.in"

Scanner sc = new Scanner(System.in);
instead of
Scanner sc = new Scanner("System.in");

Upvotes: 1

Piyush Mittal
Piyush Mittal

Reputation: 1890

remove "" from Scanner sc = new Scanner("System.in"); i.e

Scanner sc = new Scanner(System.in);

Upvotes: 1

Nir Alfasi
Nir Alfasi

Reputation: 53535

System.in is not a String!

Change:

Scanner sc = new Scanner("System.in");

to:

Scanner sc = new Scanner(System.in);

Upvotes: 3

Related Questions