0
Scanner sc = new Scanner(System.in);
System.out.println("Enter whatever you want");
String st = sc.nextLine();
try {
    String d = String.valueOf(st);
    if (d == (String) d) {
        System.out.println((String) d);
    } else {
        System.out.println(d);
    }
} catch(Exception e) {
    System.out.println("integer");
}

When I try to execute this, it keeps printing "if" part even for integer and double.

Ralf Rafael Frix
  • 1,393
  • 3
  • 21
  • 31
RKP
  • 51
  • 6
  • 1
    try to parse it to a number type and catch the exception... – ΦXocę 웃 Пepeúpa ツ Aug 02 '16 at 07:05
  • 3
    Since `d` is already a `String`, casting it to a `String` is redundant; then `d == d` is obviously always true; but checking `d == d` is redundant, since you do the same thing in both the true and false branches. And `String.valueOf(st)` is also redundant, since `st` is already a `String`, so `st.toString() == st`. – Andy Turner Aug 02 '16 at 07:06
  • To know what user has given as input you can use `instanceof`or apparently use sc.hasNextDouble() or hasNextInt() – Imran Aug 02 '16 at 07:10
  • this is also a useful link you can refer http://stackoverflow.com/questions/1102891/how-to-check-if-a-string-is-numeric-in-java – Santhucool Aug 02 '16 at 07:14
  • 2
    Possible duplicate of [Validating input using java.util.Scanner](http://stackoverflow.com/questions/3059333/validating-input-using-java-util-scanner) – Tom Aug 02 '16 at 07:23

4 Answers4

1

In order to use your code: Try to parse to integer first. If this is successful it means you have an int. If this doesn't work try parsing to a double, again if this works it means you have a double otherwise you have a string.

Use Integer.valueOf and Double.valueOf:

    System.out.println("Enter whatever you want");
    String st = sc.nextLine();
    try {
        Integer d = Integer.valueOf(st);

        System.out.println("Integer: " + d);
    } catch (NumberFormatException e) {
        try {
            Double d = Double.valueOf(st);
            System.out.println("Double: " + d);
        }catch (NumberFormatException nf) {
            System.out.println("String: " + st);
        }
    }

But I wouldn't build it this way. A better option is to use sc.hasNextInt and sc.hasNextDouble

user1121883
  • 5,153
  • 3
  • 30
  • 34
1

Any input can be evaluated as a string, even if it is "2.9". Instead, you could use Scanner's hasXYZ methods:

if (sc.hasNextInt()) {
    System.out.println("Integer");
} else if (sc.hasNextDouble()) {
    System.out.println("Double");
} else {
    System.out.println("String");
}
Mureinik
  • 252,575
  • 45
  • 248
  • 283
0

You can use this program to output the type:

import java.util.Scanner;

public class MyTest {
public static void main(String args[]) throws Exception {
    Scanner sc = new Scanner(System.in);

    System.out.println("Enter whatever you want");

    String st = sc.nextLine();
    try {
        Integer.parseInt(st);
        System.out.println("Integer");
    } catch (NumberFormatException nfe) {

        try {
            Double.parseDouble(st);
            System.out.println("Double");
        } catch (NumberFormatException nfe2) {
            System.out.println("String");
        }

    }
    sc.close();
}
}
Sudipta Roy
  • 83
  • 1
  • 5
  • 11
-1

try this:

String d= String.valueOf(st);
try{
 //If double or integer comes here
  double val = Double.parseDouble(d);
}catch(Exception e){
  //If String comes here
}
Santhucool
  • 1,615
  • 2
  • 29
  • 77