JTextField的 - 数字分离(ArrayList中) [英] JTextField - separation of numbers (ArrayList)
问题描述
原谅我,如果这个问题是太微不足道了,但我还没有找到一个确切的解决方案。
有什么办法把使用JTextField的几种不同的数字?
例如,我给2.0,3.0,4.0和我愿把它们放进ArrayList中分开。
在另一个问题,我找到了一种方法来摆脱不必要的字符,但我没有找到处理几个值。想与其他不正确的值做才能继续打字?
//在这种情况下,resulst是arList:1.92239991
文本框=新的JTextField(1.9,223,9991);
textField.addActionListener(本);
ArrayList的<双> arList =新的ArrayList<>(); 字符串str = textField.getText();
字符串结果= str.replaceAll(\\\\ S +,);
串otherResult = result.replaceAll(,,);
双D = Double.parseDouble(otherResult);
的System.out.println(D); arList.add(四);
对于(双人间anArList:arList){
的System.out.println(arList:+ anArList);
}
如果该号码始终由分离,
您可以使用字符串#分裂
,这将返回值的数组。
字符串str = textField.getText();
串[]份= str.split(,);
这将返回之间的每一个值,
,包括空格。然后,您可以修剪这些结果...
为(INT指数= 0;指数 - LT; parts.length;指数++){
部分[指数] =零件[指数] .trim();
}
这现在将仅包含剩余的文本。如果必须在列表中有它,你可以使用...
列表<串GT;值= Arrays.asList(件);
如果你需要它作为双击
列表,您将需要indiviudally解析每个元素...
列表<双>值=新的ArrayList<双>(parts.length);
对于(字符串值:值){
values.add(Double.parseDouble(值));
}
这仍然会抛出一个分析异常,如果任一值不是一个有效的双击
。
有一个更好的解决方案可能是使用一个的JFormattedTextField
或的JSpinner
来收集个人价值,并添加每一个到列表一次(可在显示他们 JList的
),这将使作为进入它,你要验证每个值。
看一看如何使用格式化的文本栏 ,如何使用纱厂并的如何使用列表的更多细节。
Forgive me if the question is too trivial, but I haven't found an exact solution. Is there any way to take several different numbers using JTextField?
For example, I give 2.0, 3.0, 4.0 and I would like to put them into ArrayList separately. In another question I found a way to get rid of unwanted characters, but I didn't find handling several values. What to do with other incorrect values in order to continue the typing?
//in this case the resulst is "arList: 1.92239991"
textField = new JTextField("1.9, 223, 9991");
textField.addActionListener(this);
ArrayList<Double> arList = new ArrayList<>();
String str = textField.getText();
String result = str.replaceAll("\\s+","");
String otherResult = result.replaceAll(",","");
double d = Double.parseDouble(otherResult);
System.out.println(d);
arList.add(d);
for (Double anArList : arList) {
System.out.println("arList: " +anArList);
}
If the numbers are always separated by ,
you can use String#split
, which will return an array of values.
String str = textField.getText();
String[] parts = str.split(",");
This will return each value between the ,
, including the white space. You could then trim these results...
for (int index = 0; index < parts.length; index++) {
parts[index] = parts[index].trim();
}
This will now contain just the remaining text. If you must have it in a list you can use...
List<String> values = Arrays.asList(parts);
If you need it as list of Double
s, you will need to parse each element indiviudally...
List<Double> values = new ArrayList<Double>(parts.length);
for (String value : values) {
values.add(Double.parseDouble(value));
}
This will still throw a parse exception if any one of the values is not a valid Double
.
A better solution might be to use a JFormattedTextField
or JSpinner
to collect the individual value and add each one to a list one at a time (could display them in a JList
), this would allow you to validate each value as it is entered.
Have a look at How to Use Formatted Text Fields, How to Use Spinners and How to Use Lists for more details
这篇关于JTextField的 - 数字分离(ArrayList中)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!