java数据输入与分辨问题
import java.util.ArrayList;
import java.util.Scanner;
public class DataParse {
public DataParse() {
Scanner scanner = new Scanner(System.in);
String str = scanner.nextLine();
ArrayList<Integer> intArr = new ArrayList<Integer>();
ArrayList<Double> dblArr = new ArrayList<Double>();
ArrayList<String> strArr = new ArrayList<String>();
while(!str.isEmpty()) {
try {
Integer e = Integer.parseInt(str);
intArr.add(e);
}catch(NumberFormatException e) {
try {
Double d = Double.parseDouble(str);
dblArr.add(d);
}catch(NumberFormatException ex) {
strArr.add(str);
}
}
str = scanner.nextLine();
}
//
System.out.println("整数***计有" + intArr.size() + "个");
for(Integer e: intArr)System.out.println(e);
System.out.println("小数***计有" + dblArr.size() + "个");
for(Double e: dblArr)System.out.println(e);
System.out.println("字符串***计有" + strArr.size() + "个");
for(String e: strArr)System.out.println(e);
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
new DataParse();
}
}
// 输入两个回车结束输入
1222
222
rr
44.66
55.7
ee
整数***计有2个
1222
222
小数***计有2个
44.66
55.7
字符串***计有2个
rr
ee