我的代码遇到了一个问题,我无法删除数组列表中的空元素。它最终返回,但是这些空元素在多次尝试后不会被删除[1, 2, 3, 0, 8, , 12 , , 34 , 0 ]
public static ArrayList<String> readNumbers() {
Scanner inFile = null;
File file = null;
String filePath = (JOptionPane.showInputDialog("Please enter a file path"));
int size = 0;
ArrayList<String> result = new ArrayList<String>();
try {
file = new File(filePath);
inFile = new Scanner(file);
int skippedCounter = 0;
for(int i = 0; inFile.hasNext(); i++){
if(inFile.hasNextInt())
result.add(inFile.next());
else{
String strOut = "";
String data = inFile.nextLine();
for(int j = 0; j <= data.length() - 1; j++){
if(!Character.isLetter(data.charAt(j))){
strOut += data.charAt(j);
}
else
skippedCounter++;
}
if(!strOut.isEmpty())
result.add(strOut);
}
}
}
catch (FileNotFoundException e) {
System.out.println("File not found");
}
catch(NumberFormatException e){
System.out.println("Not a number");
}
finally {
inFile.close();
}
int count = 0;
result.removeIf(String::isEmpty);
return result;
}
三国纷争
米脂
天涯尽头无女友
相关分类