我有一个名为" input.txt"以下是数字。我想读取这个文件并将其变成一个数组。第一个整数 - 10表示数组中索引或元素的数量,因此有10个点。下面的代码是我到目前为止所做的,我只是不知道如何从第一行读取input.txt
10
3
4
5
6
4
4
5
6
3
此数组的大小将是文件中的第一个数字
static void display(int[] numArray) {
System.out.println("Array contents: ");
for (int i = 0; i < numArray.length; i++) {
System.out.print(numArray[i] + " ");
}
System.out.println();
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int[] numberArray = new int[10];
System.out.println("Enter the name of your file (including file extension): ");
String filename = input.next();
int count = 0;
try {
Scanner in = new Scanner(new File(filename));
int result = in.nextInt();
while (in.hasNextInt()&& count < 10) {
numberArray[count] = result;
count = count + 1;
result = in.nextInt();
}
} catch (FileNotFoundException e) {
System.out.println("That file was not found. Program terminating...");
e.printStackTrace();
}
display(numberArray);
}
答案 0 :(得分:0)
这样的事情应该有效(未经测试)。注意如何首先抓取大小,基于该大小创建的数组,然后正在读取文件的其余部分。
static void display(int[] numArray) {
System.out.println("Array contents: ");
for (int i = 0; i < numArray.length; i++) {
System.out.print(numArray[i] + " ");
}
System.out.println();
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter the name of your file (including file extension): ");
String filename = input.next();
int count = 0;
int[] numberArray;
try {
Scanner in = new Scanner(new File(filename));
int size= in.nextInt();
numberArray = new int[size];
int result;
while (in.hasNextInt()&& count < size) {
numberArray[count] = result;
count = count + 1;
result = in.nextInt();
}
} catch (FileNotFoundException e) {
System.out.println("That file was not found. Program terminating...");
e.printStackTrace();
}
display(numberArray);
}
答案 1 :(得分:0)
您需要创建第一行大小的数组,如果需要,可以放入数组。您可以尝试以下方式:
如果您使用的是Java 7或更高版本,可以尝试以下方法:
public static void main(String[] args) {
try (final Scanner input = new Scanner(System.in)) {
final int[] numberArray = new int[10];
System.out.println("Enter the name of your file (including file extension): ");
final String filename = input.next();
try (final Scanner in = new Scanner(new File(filename))) {
final int count = in.nextInt();
for (int i = 0; in.hasNextInt() && i < count; i++) {
numberArray[i] = in.nextInt();
}
display(numberArray);
} catch (final FileNotFoundException e) {
System.out.println("That file was not found. Program terminating...");
e.printStackTrace();
}
}
}
如果你在7之前使用java,试试这个:
public static void main(String[] args) {
try (final Scanner input = new Scanner(System.in)) {
System.out.println("Enter the name of your file (including file extension): ");
final String filename = input.next();
int[] numberArray = null;
try (final Scanner in = new Scanner(new File(filename))) {
final int count = in.nextInt();
numberArray = new int[count];
for (int i = 0; in.hasNextInt() && i < count; i++) {
numberArray[i] = in.nextInt();
}
display(numberArray);
} catch (final FileNotFoundException e) {
System.out.println("That file was not found. Program terminating...");
e.printStackTrace();
}
}
}
如果要将第一行添加到数组中,请将numberArray[0] = count;
放在numberArray = new int[count];
下方并使用 1 初始化步骤。