我想隐藏存储在DataBase As字符串类型中的这个附加单元格 我从数据库中检索后,在我的代码中浮动数组 例如:
float [9] EX = new float[0.152, 0.253, 0.102, 0.304, 0.355, 0.355, 0.152, 0.203, 0.203];
任何人都可以帮助我! Regrads!
答案 0 :(得分:2)
首先将输入字符串分成String#split
部分,然后解析值:
String input = "0.123 0.234 0.456"; // <= string from DB cell
String[] ss = input.split("\\s+"); // <= split using whitespaces as splitter
float[] f = new float[ss.length];
int i = 0;
for(String s : ss) { // <= iterate over separate values
try {
f[i++] = Float.parseFloat(s);
} catch (RuntimeException e) {
System.out.println(s + " cannot be converted to float");
}
}
答案 1 :(得分:1)
使用 split(String regex),请参阅以下代码:
String[] arr = yourString.split(" ");
float [] floatArr = new float[arr.length];
int i=0;
for(String newString: arr){
String trimedString = newString.trim();// Just ensure you have valid string to convert.
if(trimedString.length() > 0)
floatArr[i++] = Float.parseFloat(newString);
}
System.out.println(Arrays.toString(floatArr)); // Just to print
请参阅result:
[0.152,0.253,0.102,0.304,0.355,0.355,0.152,0.203,0.203]