简体   繁体   中英

How to recognize textfiled data type java string or integer

我有文本框,我想知道用户在文本框中输入的数据类型,并且取决于类型,如果有任何功能可以知道数据类型,我想做点什么。

import org.apache.commons.lang.StringUtils;    
StringUtils.isNumeric(String s);

Notice this doesn't throw exceptions on regular cases, which should be avoided in every possible scenario.

String name= scanner.nextLine();
try{
   int integerValue = Integer.parseInt(name);
   sysout("Name is integer value")
}
catch(NumberFormatException){
  sysout("name is not an integer value");
}

By default you will get string type only, you have to write your own logic for checking datatype. you can restrict input for numberic or string values only using javascript.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM