-12

enter image description here

package test;
import java.util.*;
public class NewClass {
    public static void main (String[] args)
    {   
        String s;
        Scanner sc = new Scanner (System.in);
        s=sc.nextLine();
        System.out.println(s);
    }

}

I don't know why this error is coming.

Exception in thread "main" java.util.InputMismatchException at java.util.Scanner.throwFor(Scanner.java:864) at java.util.Scanner.next(Scanner.java:1485) at java.util.Scanner.nextInt(Scanner.java:2117) at java.util.Scanner.nextInt(Scanner.java:2076) at test.Test.main(Test.java:12) C:\Users\MOHIT KUMAR SINGH\AppData\Local\NetBeans\Cache\8.2\executor-
snippets\run.xml:53: Java returned: 1 BUILD FAILED (total time: 2 seconds)

Milo
  • 3,365
  • 9
  • 30
  • 44
  • 2
    This code does not seem to bear much relation to the error in your stack trace. – khelwood Jun 01 '18 at 11:56
  • still same error. – Mohit Kumar singh Jun 01 '18 at 11:57
  • 2
    No offense meant, but you could have made some research on your own before asking the whole community for that kind of simple problem. – sjahan Jun 01 '18 at 11:57
  • I already did but not finding any of it useful. – Mohit Kumar singh Jun 01 '18 at 11:59
  • First, your stacktrace does not match your code: in your code, you use `nextLine` though there is only `nextInt` in the stacktrace. It's not the same class name too... Then, you take a look at the doc: https://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html#nextInt() : InputMismatchException - if the next token does not match the Integer regular expression, or is out of range. This explains the exception. – sjahan Jun 01 '18 at 12:02
  • Please read [mcve] and enhance your question accordingly. – GhostCat Jun 01 '18 at 12:03
  • Possible duplicate of [Why am I getting InputMismatchException?](https://stackoverflow.com/questions/14027537/why-am-i-getting-inputmismatchexception) – sjahan Jun 01 '18 at 12:08
  • `nextLine` doesn't even throw the exception you mention... – sjahan Jun 01 '18 at 12:15
  • @sjahan I copied and pasted the same error i am getting. – Mohit Kumar singh Jun 01 '18 at 12:17
  • @MohitKumarsingh I think you do not get it. As I already told you: the code and the stacktrace does not match. The error is caused by a call to `nextInt` which does not exist in your code, which means you don't actually show your code... Or you don't know what you actually run. **Once again, `nextLine` does not throw InputMismatchException** – sjahan Jun 01 '18 at 12:19
  • Ok, I can confirm what I just said: you don't know what you actually run. Here, this is the `Test` class you run, not the `NewClass`. – sjahan Jun 01 '18 at 12:24
  • @sjahan ,Now i get it . thanks a lot – Mohit Kumar singh Jun 01 '18 at 12:28

2 Answers2

0

java.util.InputMismatchException-In order to deal with this exception you must verify that the input data of your application meet its specification. When this error is thrown, the format of the input data is incorrect and thus, you must fix it, in order for your application to proceed its execution.

Sweta Patra
  • 341
  • 1
  • 4
0

I think it's your main class but there no argument/parameter in your main method. You should do like this

public static void main(String[] arr) {
    Scanner sc = new Scanner(System.in);
    String s = sc.nextLine();
    System.out.println(s);
}