我对Java很陌生,而且我在打印通过读取.txt文件创建的数组时遇到了困难。我无法弄清楚我做错了什么。另外,我如何将.txt文件放入与我的类相同的目录中?这些必须是单独的方法,
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;
public class ArrayOperations {
// The code for the method readFromFile is given below.
public static void main(String[] args){
}
public static ArrayList<Integer> readFromFile(String fileName)
throws FileNotFoundException
{
File f = new File("data.txt");
Scanner fileIn = new Scanner (f);
ArrayList<Integer> list = new ArrayList<Integer>();
while (fileIn.hasNextInt()){ // quit when you encounter ‘Q’
int num = fileIn.nextInt();
list.add(num);
}// end while
fileIn.close();
return list;
} // end readFromFile
public static String printArray(int[] readFromFile){
String str = "";
for(int i = 0; i < 10; i++){
str = str + Integer.toString(readFromFile[i]) + " ";
}
return str;
}
}
这是名为Data.txt的.txt文件
我想要获得的输出是:
文件中的数据是:
45 32 97 87 64 37 65 72 84 22 58 65 72 89 93 95
答案 0 :(得分:0)
UIApplicationDelegate