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);
    }
}

Samuel Liew
  • 68,352
  • 105
  • 140
  • 225
user158302
  • 27
  • 5
  • String s=scan.next(); – koding Jan 04 '20 at 14:23
  • @user158302 can you tell me why you import the Scanner class separately?? – Akash Jan 04 '20 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()?](https://stackoverflow.com/q/13102045) for more details). – Pshemo Jan 04 '20 at 14:57
  • Anyway to get proper answers we need to [edit] your question and provide proper [MCVE] (a.k.a. [SSCCE](http://sscce.org)) which aside from code also should include input you used while running your code and *exact* error message you are getting. – Pshemo Jan 04 '20 at 14:58

4 Answers4

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

Ashish Modi
  • 6,282
  • 2
  • 14
  • 26
Harsh Yadav
  • 136
  • 4
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();
WJS
  • 22,083
  • 3
  • 14
  • 32
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);
    }

}
ranjith
  • 369
  • 1
  • 9
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();
snr
  • 13,515
  • 2
  • 48
  • 77