0

I am just trying to read integer, double , string values from keyboard. it just working fine for integer and double. but when it comes to string, it was throwing a Input Mismatch Exception, so i cant able to read string from keyboard.

import java.util.Scanner;
import java.util.*;

public class Solution {

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int i = scan.nextInt();
        double d= scan.nextInt();
        String s=scan.nextLine();

        // Write your code here.

        System.out.println("String: " + s);
        System.out.println("Double: " + d);
        System.out.println("Int: " + i);
    }
}

4
  • String s=scan.next(); Commented Jan 4, 2020 at 14:23
  • @user158302 can you tell me why you import the Scanner class separately?? Commented Jan 4, 2020 at 14:23
  • "but when it comes to string, it was throwing a Input Mismatch Exception," is very had to believe since string can represent any characters provided by users so it shouldn't be mismatched. Other problem is that nextLine() most likely will return empty string (see Scanner is skipping nextLine() after using next() or nextFoo()? for more details). Commented Jan 4, 2020 at 14:57
  • Anyway to get proper answers we need to edit your question and provide proper minimal reproducible example (a.k.a. SSCCE) which aside from code also should include input you used while running your code and exact error message you are getting. Commented Jan 4, 2020 at 14:58

4 Answers 4

1

You have used double d= scan.nextInt();

instead use

double d= scan.nextDouble();

Moreover String is working fine for following input and output: 1 1 Hello

String: Hello

Double: 1.0

Int: 1

Sign up to request clarification or add additional context in comments.

2 Comments

I think it has nothing to do with the object type since the result of the input is promoted automagically to double.
1 1.0 Hello Exception in thread "main" java.util.InputMismatchException
0

First, you're doing a scan.nextInt() for a double. Not the best thing to do. Second, try scan.next() to get an arbitrary type token.

        int i = scan.nextInt();
        double d= scan.nextDouble();
        String s=scan.next();

Comments

0

please check the below code. it is working for me.

      public class Solution {

    public static void main(String[] args) {

        Scanner scan = new Scanner(System.in);
        System.out.println("enter i value ");
        int i = scan.nextInt();
        System.out.println("enter d value ");
        double d= scan.nextDouble();
        System.out.println("enter s value ");
        String s=scan.next();


        // Write your code here.

        System.out.println("String: " + s);
        System.out.println("Double: " + d);
        System.out.println("Int: " + i);
    }

}

Comments

0

I think String s=scan.nextLine(); consumes the newline character coming from the followed calling double d= scan.nextInt();.

Here is my two cents.

double d = scan.nextInt();
scan.nextLine();
String s = scan.nextLine();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.