I'm trying to pa开发者_如何学JAVArse the string value "87306E107" using hasNextDouble. This value is a string and should return false when using hasNextDouble but it returns true. I want know if it's possible to have this method return false? The reason I want to treat this value as a string is because I have to wrap it in single quotes for building a dynamic database insert statement. Here is my code below:
String data = "87306E107,27.1,xyz,123,01449J204";
Scanner scanner = new Scanner(data);
scanner.useDelimiter(",");
if (!scanner.hasNextInt() && !scanner.hasNextDouble()){
if (DateUtils.isDate(data)){
//Format date to correct layout
data = DateUtils.parseStringDate(data, "yyyy-MM-dd", 0);
}
//Escape any single quotes
data = data.replaceAll("'", "''");
//Wrap in single quotes
data = "'" + data + "'";
}
You can try a string matcher instead of hasNextDouble()
:
scanner.hasNext("\\d+(\\.\\d+)?")
精彩评论