java: I want to know the primitive datatype of input which is stored in String variable -


string x; x=scanner.nextline(); 

i entered 1 or 6.66. should give output int or double/float. whatever necessary.

instead of parsing result of scanner.nextline() returns string, should instead use convenience methods of scanner class hasnextint() , hasnextfloat().

for example following code should expect :

if (scanner.hasnextfloat()) {     system.out.println("float");     float myvalue = scanner.nextfloat();     // float myvalue }else if (scanner.hasnextint()) {     system.out.println("int")     int myvalue = scanner.nextint();     // int myvalue } else { // fallback on string     string myvalue = scanner.next();     // string myvalue } 

Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -