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
Post a Comment