【发布时间】:2018-12-09 06:24:26
【问题描述】:
所以基本上我可以使用缓冲阅读器阅读文本文件。 之后我应该 SPLIT 和 PARSE 它,在这一点上,我面临一个问题。
输出是: 5 2 9 1 7 3 9 2 10 11 6 3
线程“main”java.lang.NumberFormatException 中的异常:对于输入字符串:“F:\Gephi\number.txt” 在 java.lang.NumberFormatException.forInputString(NumberFormatException.java:65) 在 java.lang.Integer.parseInt(Integer.java:580) 在 java.lang.Integer.parseInt(Integer.java:615) 在 javaapplication17.JavaApplication17.main(JavaApplication17.java:42) C:\Users\Asus\AppData\Local\NetBeans\Cache\8.2\executor-sn-ps\run.xml:53:Java 返回:1 构建失败(总时间:0 秒)
我的密码是
package javaapplication17;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class JavaApplication17 {
public static void main(String[] args) {
String count = "F:\\Gephi\\number.txt";
try (BufferedReader br = new BufferedReader(new FileReader(count)))
{
String line;
while ((line = br.readLine()) != null)
{
System.out.println(line);
}
}
catch (IOException e)
{
e.printStackTrace();
}
// What is the Problem in this part? //
String[] abc = count.split("/t");
int[] intArray = new int[abc.length];
for (int i=0; i<abc.length; i++)
{
String numberAsString = abc[i];
intArray[i] = Integer.parseInt(numberAsString);
}
System.out.println("Number of integers: " + intArray.length);
System.out.println("The integers are:");
for (int number : intArray)
{
System.out.println(number);
}
}
}
我的文本文件是这样的
5 6 7
5 2 9
1 7 3
9 2 10
11 6 3
【问题讨论】:
-
试试
Integer.parseInt(numberAsString.trim()); -
你想怎么拆分
count?这个String[] abc = count.split("/t"); -
应该是这样
while ((line = br.readLine()) != null) { line += line; }然后用String[] abc = line.split("/t"); -
@HadiJ 我试过了,先生,但还是卡住了:(
-
String numbers = "",line; try (BufferedReader br = new BufferedReader(new FileReader(count))) { while ((line = br.readLine()) != null) { numbers+=line; } } catch (IOException e) { }