0

上周我一直在试图弄清楚如何让这个愚蠢的代码工作。除了从我的文本文件中读取之外,我已经设法让一切正常工作。它可以读取一行上的单个整数,但是当给定一行包含多个用空格分隔的整数时,它就吓坏了。现在我已经去尝试修复它,代码甚至不再编译了。只有一条线引起了问题。我不擅长编码,所以我不知道从哪里开始。是的,我在网上查过这个。是的,我已经检查了论坛。是的,我已经尝试了多种不同的方法来完成这项工作......我该如何解决这个问题?:(

ArrayList<Integer> list = new ArrayList<Integer>();
// the above line is in a different method in the same class, but it's relevant here


File file = new File("C:\\Users\\Jocelynn\\Desktop\\input.txt");
    BufferedReader reader = null;

    try
    {
        reader = new BufferedReader(new FileReader(file));
        String text = null;


        while ((text = reader.readLine()) != null)
        {
            // I want the following line to read "218 150 500 330", and to store each individual integer into the list. I don't know why it won't work :(
            list.add(Integer.parseInt(src.next().trim()));
        }
    } 
    catch (FileNotFoundException e)
    {
    e.printStackTrace();
    } 
    catch (IOException e) 
    {
    e.printStackTrace();
    } 
    try
    {
   reader.close();
    }
    catch (IOException e) 
    {
    e.printStackTrace();
    }


//print out the list
System.out.println(list);

感谢您的帮助!我确定我只是错过了一些非常简单的东西......

4

3 回答 3

1

你可以用一个Scanner(String)like

while ((text = reader.readLine()) != null) {
    Scanner scanner = new Scanner(text);
    while (scanner.hasNextInt()) {
        list.add(scanner.nextInt());
    }
}

当然,您的整个方法可以通过使用try-with-resourcesStatement和 anddiamond operator来简化,Scanner(File)就像

public static void main(String[] args) {
    File file = new File("C:\\Users\\Jocelynn\\Desktop\\input.txt");

    List<Integer> list = new ArrayList<>();
    try (Scanner scanner = new Scanner(file);) {
        while (scanner.hasNextInt()) {
            list.add(scanner.nextInt());
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    // print out the list
    System.out.println(list);
}
于 2014-10-26T03:52:25.697 回答
0

在while循环中执行此操作

String[] individualArray = text.split(" ");//note space
for(String individual:individualArray){
    yourList.add(individual);//You need to parse it to integer here as you have already done
}

在上面的代码中,individualArray将包含每个单独的整数,即separated by space. 在for 循环中,每个字符串都需要解析为整数,然后添加到您的列表中

于 2014-10-26T03:47:15.570 回答
0

尝试这个 :

public static void main(String[] args) {

        ArrayList<Integer> list = new ArrayList<Integer>();
        File file = new File("C:\\Users\\Jocelynn\\Desktop\\input.txt");
        BufferedReader reader = null;

        try
        {
            reader = new BufferedReader(new FileReader(file));
            String text = null;

            while ((text = reader.readLine()) != null)
            {
                // you need only this for loop in you code.
                for (String value : text.split(" ")) {  // get list of integer
                     if(!value.equals(""))             // ignore space                      
                     list.add(Integer.parseInt(value));  // add to list
                }

            }
        } catch (FileNotFoundException e)
        {
            e.printStackTrace();
        } catch (IOException e)
        {
            e.printStackTrace();
        }
        try
        {
            reader.close();
        } catch (IOException e)
        {
            e.printStackTrace();
        }

        // print out the list
        System.out.println(list);

    }
于 2014-10-26T04:18:51.127 回答