0

我是编程新手,在获取带有数字列表的文件并将其转换为整数数组时遇到了一些麻烦,然后我可以在格式化的列(5 行和 10 列)中打印。我想我使用 an 进行了正确的导入,ArrayList但是当我尝试打印列时遇到了问题。我想我需要使用 for 循环来打印列,但我不是 100% 确定。任何数量的帮助将不胜感激!这是我的代码:

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class Numbers {

    private List < Integer > data = new ArrayList < Integer > (); //Create ArrayList

    public static void main(String[] args) {
        String filename = "C/Users/s/Desktop/file4/Input.txt";

        Numbers rfe = new Numbers();
        rfe.readFile(filename);
    }



    private void readFile(String name) {
        String input;

        try (BufferedReader reader = new BufferedReader(new FileReader(name))) {

            while ((input = reader.readLine()) != null) {
                data.add(Integer.parseInt(input)); //Add each parsed number to the arraylist
                int[] i = input;

                for (i; i < null; i++) {
                    System.out.format("%20s, %10s", i);
                }

            }
        } catch (FileNotFoundException fnfe) {

        } catch (IOException ioe) {

        } catch (Exception ex) { // Not required, but a good practice
            ex.printStackTrace(); //Usually good for general handling
        }
    }
}

这是文件包含的内容(一行中的每个数字):

32
73
63
47
72
34
26
84
27
75
95
10
48
88
28
65
71
40
14
11
67
76
77
80
12
15
30
74
13
41
21
22
57
17
99
92
52
38
18
46
62
64
39
16
43
29
79
49
19
60
4

3 回答 3

0

首先,您必须读取文件,然后将其添加到 Arraylist。现在您可以轻松地显示这个,我将 arraylist 中的每个值逐一添加到多维数组中。

ArrayList<Integer> data = new ArrayList<Integer>();

    String numberString="";

    try {
        BufferedReader br = new BufferedReader(new FileReader(fileName));
    /*  while ((numberString = br.readLine()) != null) {

        }*/
        numberString=br.readLine();
        System.out.println(numberString);
        String[] numbersArray = numberString.split(" ");
        for(int i=0;i<numbersArray.length;i++){
            //System.out.println("array value-->"+numbersArray[i]);
            data.add(Integer.parseInt(numbersArray[i]));
        }

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
于 2015-10-04T17:41:10.753 回答
0

在下面,我写并解释了您尝试做的事情。

public static void main(String[] args) throws IOException {
    File file = new File("C/Users/s/Desktop/file4/Input.txt");
    try (BufferedReader br = new BufferedReader(new FileReader(file))) {
        String line;
        List<Integer> intArr = new ArrayList<Integer>();
        while ((line = br.readLine()) != null) { //reads the Input.txt line by line
           intArr.add(Integer.valueOf(line)); //adds int values(read from txt file) into ArrayList
           System.out.format("%10s", Integer.valueOf(line)); //it can print in your format while reading the integers
        }
        for (Integer integer : intArr) {
               System.out.format("%10s", integer); // or prints in a separate loop.
        }
    }
}
于 2015-10-04T17:45:53.270 回答
0

您的代码中有两个问题。然后在这部分:

while ((input = reader.readLine()) != null) {
    data.add(Integer.parseInt(input)); //Add each parsed number to the arraylist
    int[] i = input;

    for (i; i < null; i++) {
        System.out.format("%20s, %10s", i);
    }
}

首先你做对了,你已经将读取的数字添加到你的列表中:data.add(Integer.parseInt(input));这就是你所需要的,将文件中的数字添加到你的列表中,所以你只需要这个:

while ((input = reader.readLine()) != null) {
    data.add(Integer.parseInt(input)); //Add each parsed number to the arraylist
}

之后你想打印它,你看到的错误是因为你做了错误的分配int[] i = input;忘记了这一点,你不需要它。

您需要遍历列表以打印所有数字

for (int i=0; i<data.size(); i++){
    System.out.format("%20s, %10s", i); //here is your another problem
}

您想在该行上打印两个参数"%20s, %10s",但您只提供一个,因此要么只打印一个,要么传递i两次:

System.out.format("%20s, %10s", i, i); //twice

或者

System.out.format("%10s", i); //just once
于 2015-10-04T17:27:05.120 回答