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 starts eclipse shows 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)
System.in
is not a string!
Edit:
Scanner sc = new Scanner("System.in");
in
Scanner sc = new Scanner(System.in);
Remove double quote "from" System.in "
Scanner sc = new Scanner(System.in);
instead of
Scanner sc = new Scanner("System.in");
remove ""
from Scanner sc = new Scanner("System.in");
iee
Scanner sc = new Scanner(System.in);
you passed an invalid parameter in the constructor, the constructor syntax for the input stream is
public Scanner(InputStream source)
Creates a new scanner that produces values ββscanned from the specified input stream. The bytes from the stream are converted to characters using the platform's default base encoding. Parameters:
source
- input stream for scanning
example: Scanner sc = new Scanner(System.in);