【问题标题】:How to read multiple data type file into ArrayList using Scanner如何使用 Scanner 将多种数据类型文件读入 ArrayList
【发布时间】:2012-03-10 23:35:42
【问题描述】:

我正在尝试使用带有分隔符“\s\s”的扫描仪将多个数据类型的文件读入 ArrayList 对象,但它似乎没有按预期工作。我正在使用 printf 来查看数据是否可以正确存储,这些数据我将在以后用于计算。数据似乎正确显示,但我仍然收到“不正确的文件格式”异常。循环似乎也有问题。使用对象的 ArrayList 时总是卡住。

示例文本文件:

item  item descr  100  1.50
item2  item descr  250  2.50
item2  item descr  250  3.50

代码:

import java.io.*;
import java.util.*;

public class ReadItems
{

    private Scanner input;
    ArrayList<Item> item = new ArrayList<Item>();

    //open text file
    public void openFile()
    {
        try
        {
            FileReader in = new FileReader("Items.txt");
            input = new Scanner(in).useDelimiter("\\s\\s");
        }
        catch( FileNotFoundException fileNotFound)
        {
            System.err.println( "Error opening file.");
            System.exit(1);
        }
    }

    //read file
    public void readFile()
    {
        try
        {
            while ( input.hasNextLine())
            {
                item.add( new Item(input.next(), input.next(), input.nextInt(), input.nextFloat() ));                                       
                for (Item list : item) 
                {
                    System.out.printf("%-10s%-48s$%5.2f\n", list.getCode(), (list.getDecription()+ ", "+ list.getWeight()+ "g"), + list.getPrice());
                    //System.out.println(item);
                }

            }

        }
        catch ( NoSuchElementException elementEx)
        {
            System.err.println( "Incorrect file format.");
            System.exit(1);
        }
        catch ( IllegalStateException stateEx )
        {
            System.err.println( "Error reading from file.");
            System.exit(1);
        }

    }

    public void closeFile()
    {
        if (input != null)
            input.close();      
    }

}

输出:

item      item descr, 100g                                $ 1.50
item      item descr, 100g                                $ 1.50
item2     item descr, 250g                                $ 2.50
item      item descr, 100g                                $ 1.50
item2     item descr, 250g                                $ 2.50
item2     item descr, 250g                                $ 3.50

文件格式不正确。

抱歉,我似乎在做一件愚蠢的事情。我没有通过main 所在的测试类运行程序。

测试类:

public class TestReadItems
{

public static void main(String[] args) 
{
ReadItems application = new ReadItems();
application.openFile();
application.readFile();
application.closeFile();
}
}

程序运行没有错误,但我似乎无法让 while 循环正常工作。产量翻了三倍。

【问题讨论】:

    标签: java arraylist java.util.scanner


    【解决方案1】:

    这是因为用于打印输出的 for 循环位于 while 循环内。因此,它读取文件的每一行并返回输出。因此,要更正,请替换 while 语句中的输出 for 循环,并在 while 循环完成后编写它。

    【讨论】:

    • 是的,我看着这个SSCCE,我在想我是写这个还是我问了这个问题?几个月内可以学到多少东西真是令人惊讶:)
    【解决方案2】:

    循环也可能因文件末尾的垃圾而崩溃。我在 item.add() 调用之后添加了对 .nextLine() 的调用,它现在对我来说很好。

    while ( input.hasNextLine()) {
        item.add( new Item(input.next(), input.next(), input.nextInt(), input.nextFloat() ));                                       
        for (Item list : item) {
            System.out.printf("%-10s%-48s$%5.2f\n", list.getCode(), (list.getDecription()+ ", "+ list.getWeight()+ "g"), + list.getPrice());
        }
        input.nextLine(); // added
    }
    

    【讨论】:

    • \\s 实现所有空白字符(空格、制表符、换行符)?因此我相信 \n 不会有所作为。我也在使用input.hasNextLine()。我试过了,结果一样。 while 循环中有任何 cmets 吗?
    • 我的评论有点慢。这对你有什么作用?为什么会呢?嗯
    • 我已经尝试将分隔符更改为 \\s\\s|\n,因为这是我的第一个想法,但没有任何区别。
    • 我建议的更改对我有效,但对您无效,这表明示例数据中未显示空格的一些差异。你能附上你的 Sample.txt,或者把它上传到我们可以看到的地方吗?
    • 为简单起见,我在测试中只使用了一行
    猜你喜欢
    • 1970-01-01
    • 2016-05-13
    • 1970-01-01
    • 1970-01-01
    • 2020-11-22
    • 1970-01-01
    • 1970-01-01
    • 2019-04-14
    • 2020-01-02
    相关资源
    最近更新 更多