-1
import java.util.*;
import java.lang.*;
import java.io.*;
class GFG
 {
    public static void main (String[] args)
     {
        String s;
        int n;
        float f;
        Scanner obj=new Scanner(System.in);
        System.out.println("Enter Your Test Case");
        int t=obj.nextInt();
        for(int i=1;i<=t;i++)
        {
            System.out.println("Enter a String value");
            s=obj.next();
            System.out.println("Enter A integer value");
            n=obj.nextInt();
            System.out.println("Enter a float value");
            f=obj.nextFloat();
            System.out.println(s+" "+n+" "+f);
        }


     }
}

This program is perfectly working on my system, but I'm getting an exception on GeeksForGeeks

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 GFG.main(File.java:13)
Mark Rotteveel
  • 82,132
  • 136
  • 114
  • 158
  • 2
    `InputMismatchException` means the input value does not match the type of `next***`. – xingbin Sep 28 '18 at 06:52
  • what values you are entering – Dinesh Sep 28 '18 at 06:55
  • I am guessing that at scenario where this error is thrown, when you are sing for String, there is provided empty string or string containing only whitespaces. This will cause `next()` to ignore it (because whitespace is delimiter and that method returns token - part separated by delimiters) and wait for non-delimiter characters. Because of that `next()` will consume *value* ment for `nextInt()` but `nextInt()` call will try to consume value for `nextFloat()` which may cause the exception. – Pshemo Sep 28 '18 at 06:56
  • Following @Pshemo's comment: It would probably be a good idea to wrap the scanner code in a `try ... catch`, get the original input String, trim it and throw an `InvalidArgumentException` if it's empty. – Agi Hammerthief Sep 28 '18 at 07:00
  • @AgiHammerthief Problem here is that empty string will not be accepted by `next()`. If application *should* be able to accept it OP would need to use `nextLine` instead. It would also require handling problem described at [Scanner is skipping nextLine() after using next() or nextFoo()?](https://stackoverflow.com/q/13102045) but this looks like correct approach. – Pshemo Sep 28 '18 at 07:04
  • @Pshemo: That's probably why I was taught to not use `Scanner` for reading CLI input ... :-? – Agi Hammerthief Sep 28 '18 at 07:09

2 Answers2

0

The InputMismatchException is thrown when attempting to retrieve a token using the text Scanner class that doesn’t match the expected pattern or type. Please check the input.

nbirla
  • 592
  • 3
  • 13
0
System.out.println("Enter a String value");
s=obj.nextLine();

Try the above code. next() and nextLine() have differences. I don't know what your input is, but give it a try. More information.

Agi Hammerthief
  • 1,915
  • 1
  • 19
  • 32
Arty
  • 721
  • 9
  • 26
  • 1
    While it is proper direction, this will always return empty string (at least if there will be no other chances in code). See [Scanner is skipping nextLine() after using next() or nextFoo()?](https://stackoverflow.com/q/13102045) – Pshemo Sep 28 '18 at 07:03