【发布时间】:2019-03-31 23:04:53
【问题描述】:
如果文本文件由数字行组成,则下面的代码运行得非常好,但是一旦到达例如“我是 40”的行,它就会跳过它而不是将 40 放入数组中。
Scanner inFile = null;
File file = null;
String filePath = (JOptionPane.showInputDialog("Please enter a file path"));
int size = 0;
int[] result = new int[10];
try {
file = new File(filePath);
inFile = new Scanner(file);
int skippedCounter = 0;
for(int i = 0; inFile.hasNext(); i++){
if(inFile.hasNextInt())
result[i] = inFile.nextInt();
else{
String strOut = "";
String data = inFile.next();
for(int j = 0; j <= data.length() - 1; j++){
if(!Character.isLetter(data.charAt(j))){
strOut += data.charAt(j);
}
else
skippedCounter++;
}
result[i] = Integer.parseInt(strOut);
}
}
}
【问题讨论】:
-
您是否尝试调试过您的代码?