我正在尝试将文件中的行从字符串分解为字符串数组,每个字符串都有一个数字。然后我试图将每个字符串转换为整数值以用于我的代码。它给了我一个数字格式异常。有什么方法我做错了吗?
16.0 0.0 30.0 0.0 0.0 30.0 0.0 10.0 30.0 8.0 16.0 54.0 16.0 0.0 54.0 16.0 0.0 54.0 0.0 0.0 54.0 0.0 10.0 54.0 5 1 5 4 3 2 5 6 7 8 9 10 4 7 2 3 8split 适用于边缘 线程“AWT-EventQueue-0”中的异常 java.lang.NumberFormatException:对于输入字符串:“5 1 5 4 3 2” 在 java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:65) 在 java.base/java.lang.Integer.parseInt(Integer.java:652) 在 java.base/java.lang.Integer.parseInt(Integer.java:770)
for (int i = 0; i < Transformation.numPoly; i ++) {
String line = Transformation.scanner.nextLine(); // file from another object.
System.out.println(line); // ensure line is read.
String[] theseEdges = line.split(" "); // split along spaces.
System.out.println("split works for edges");
int numEdges = Integer.parseInt(theseEdges[0].trim());// find first number of of string to use for program.
System.out.println(Arrays.toString(theseEdges)); //
它给了我一个数字格式异常。它在错误消息中读取的字符串中的间距与文件中或代码打印的不同。
你有一些由制表符分隔的数字。如果要拆分 any 空格,可以使用
\s
正则表达式元字符:
String[] theseEdges = line.split("\\s+"); // split along whitespaces