从文件中读取Java数组

时间:2021-03-01 15:41:33

Say i have a file called "input.txt" that has a bunch of positive integers in it:

假设我有一个名为“input”的文件。txt"里面有很多正整数

6
5
6
8
6
2
4

6 5 6 8 6 2 4。

and so on....(one integer per line)

等等....(每行一个整数)

I want to read this file and make it into an array. The first integer (in this case 6) tells the number of indexes or elements in the array, so 6 spots. The other numbers fill in the array starting at 0. So at index 0, the number is 5, at index 1 the number is 6, and so on.

我想要读取这个文件并把它变成一个数组。第一个整数(在本例中为6)告诉数组中的索引或元素的数量,所以是6个点。其他数字填充从0开始的数组。在索引0中,数字是5,在索引1中数字是6,以此类推。

Can someone please show me how to read this file and make it into an array called A and return the integers in each index as n?

有人能告诉我如何读取这个文件并使它成为一个名为A的数组并返回每个索引中的整数作为n吗?

this is what i have so far:

这是我目前所拥有的:

import java.io.*;
public class inputFile {
    public static jobScheduleRecursive(int[] A, int i)
    {
        try
    {
        FileReader filereader = new FileReader("input.txt");
        BufferedReader bufferedreader = new BufferedReader(filereader);
        String line = bufferedreader.readLine();
        //While we have read in a valid line
        while (line != null) {
            //Try to parse integer from the String line
            try {
                System.out.println(Integer.parseInt(line));
            } catch (NumberFormatException nfe) {
                System.err.println("Failed to parse integer from line:" + line);
                System.err.println(nfe.getMessage());
                System.exit(1);
            }
            line = bufferedreader.readLine();
        }
    }
    catch(FileNotFoundException filenotfoundexception)
    {
        System.out.println("File not found.");
    }
    catch(IOException ioexception)
    {
        System.out.println("File input error occured!");
        ioexception.printStackTrace();
    }
    return A;
}

1 个解决方案

#1


5  

Step by step (I will let you fill in the actual code):

一步一步(我将让你填写实际的代码):

  1. read the first integer (the java.util.Scanner can be used to read the next integer) into a variable (let's call it numberOfInts)
  2. 读取第一个整数(java.util)。扫描器可用于读取下一个整数)到一个变量中(我们称之为numberOfInts)
  3. Create an array called A with numberOfInts elements
  4. 使用numberOfInts元素创建一个名为A的数组
  5. In a loop, counting from 0 to numberOfInts - 1 (using index variable i):
    • read the next integer from the file
    • 从文件中读取下一个整数
    • set A[i] to be that integer you just read
    • 将[i]设为刚才读取的整数
  6. 在循环中,从0到numberOfInts - 1(使用索引变量i):从文件集合a [i]中读取下一个整数,使其成为刚才读取的那个整数

Here are some references:

以下是一些参考:

http://download.oracle.com/javase/1,5,0/docs/api/java/util/Scanner.html

http://download.oracle.com/javase/1 5 0 / docs / api / java / util / Scanner.html

http://download.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html

http://download.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html

#1


5  

Step by step (I will let you fill in the actual code):

一步一步(我将让你填写实际的代码):

  1. read the first integer (the java.util.Scanner can be used to read the next integer) into a variable (let's call it numberOfInts)
  2. 读取第一个整数(java.util)。扫描器可用于读取下一个整数)到一个变量中(我们称之为numberOfInts)
  3. Create an array called A with numberOfInts elements
  4. 使用numberOfInts元素创建一个名为A的数组
  5. In a loop, counting from 0 to numberOfInts - 1 (using index variable i):
    • read the next integer from the file
    • 从文件中读取下一个整数
    • set A[i] to be that integer you just read
    • 将[i]设为刚才读取的整数
  6. 在循环中,从0到numberOfInts - 1(使用索引变量i):从文件集合a [i]中读取下一个整数,使其成为刚才读取的那个整数

Here are some references:

以下是一些参考:

http://download.oracle.com/javase/1,5,0/docs/api/java/util/Scanner.html

http://download.oracle.com/javase/1 5 0 / docs / api / java / util / Scanner.html

http://download.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html

http://download.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html