我的程序存在问题,涉及数组列表、缓冲读取器、方法以及对 Java 工作原理的总体遗忘

发布于 2024-09-24 17:38:41 字数 1031 浏览 6 评论 0原文

我在执行一整天的程序时遇到了困难。我正在尝试读取一个文本文件并一次读取每一行。取出该行并创建该行单词的数组列表。然后使用数组列表的索引来定义术语。

public class PCB {

    public static void main(String arg[]) {
        read();
    }

    public static ArrayList read() {    
        BufferedReader inputStream = null;
        ArrayList<String> tokens = new ArrayList<String>();
        try {
            inputStream = new BufferedReader(new FileReader("processes1.txt"));

            String l;
            while ((l = inputStream.readLine()) != null) {
                Scanner tokenize = new Scanner(l);
                while (tokenize.hasNext()) {
                    tokens.add(tokenize.next());
                }
                return tokens;
            }
        } catch (IOException ioe) {
            ArrayList<String> nothing = new ArrayList<String>();
            nothing.add("error1");
            System.out.println("error");
            //return nothing;
        }
        return tokens;
    }
}

我收到的错误是它只读取第一行。我做错了什么? 提前非常感谢

I am having difficulties with a program that I have been working on all day. I am trying to read a text file and read each line one at a time. Take that line and make an arraylist of the words of the line. then using the index of the arraylist define terms with it.

public class PCB {

    public static void main(String arg[]) {
        read();
    }

    public static ArrayList read() {    
        BufferedReader inputStream = null;
        ArrayList<String> tokens = new ArrayList<String>();
        try {
            inputStream = new BufferedReader(new FileReader("processes1.txt"));

            String l;
            while ((l = inputStream.readLine()) != null) {
                Scanner tokenize = new Scanner(l);
                while (tokenize.hasNext()) {
                    tokens.add(tokenize.next());
                }
                return tokens;
            }
        } catch (IOException ioe) {
            ArrayList<String> nothing = new ArrayList<String>();
            nothing.add("error1");
            System.out.println("error");
            //return nothing;
        }
        return tokens;
    }
}

The error I am getting is it only reads the first line. What am I doing wrong?
Thank you so much in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

又怨 2024-10-01 17:38:41

你有“返回令牌”;在你的 while 循环中。看来提前返回会有效地切断第一线的处理。

You have "return tokens;" in your while loop. Seems like that early return would effectively cut off processing on the first line.

浅浅 2024-10-01 17:38:41

尝试将循环更改为以下内容。请注意我如何移动 return 语句。

while ((l = inputStream.readLine()) != null) {
    Scanner tokenize = new Scanner(l);
    while (tokenize.hasNext()) {
        tokens.add(tokenize.next());
    }
}
return tokens; // <-- outside the loop

编辑:如果您想读取整个文件并将每行的标记存储在单独的数组中,那么您可以创建一个 ArrayListArrayList >。

public static ArrayList<ArrayList<String>> tokenizeFile(string filename) {    
    BufferedReader inputStream = new BufferedReader(new FileReader(filename));
    ArrayList<ArrayList<String>> lines = new ArrayList<ArrayList<String>>();

    while (true) {
        String line = inputStream.readLine();
        if (line == null) break;

        ArrayList<String> tokens = new ArrayList<String>();
        Scanner tokenizer = new Scanner(line);
        while (tokenizer.hasNext()) {
            tokens.add(tokenizer.next());
        }
        lines.Add(tokens);
    }
    return lines;
}

注意:我的 Java 生锈了。

Try changing your loop to the following. Note how I moved the return statement.

while ((l = inputStream.readLine()) != null) {
    Scanner tokenize = new Scanner(l);
    while (tokenize.hasNext()) {
        tokens.add(tokenize.next());
    }
}
return tokens; // <-- outside the loop

Edit: If you want to read the entire file and store the tokens of each line in a seperate array, then you could create an ArrayList of ArrayList.

public static ArrayList<ArrayList<String>> tokenizeFile(string filename) {    
    BufferedReader inputStream = new BufferedReader(new FileReader(filename));
    ArrayList<ArrayList<String>> lines = new ArrayList<ArrayList<String>>();

    while (true) {
        String line = inputStream.readLine();
        if (line == null) break;

        ArrayList<String> tokens = new ArrayList<String>();
        Scanner tokenizer = new Scanner(line);
        while (tokenizer.hasNext()) {
            tokens.add(tokenizer.next());
        }
        lines.Add(tokens);
    }
    return lines;
}

Note: My Java is rusty.

夏有森光若流苏 2024-10-01 17:38:41

简化为......

        String l;
        while ((l = inputStream.readLine()) != null) {
            tokens.addAll(Arrays.asList(l.split(" ")));
        }

创建文件中所有行上的所有标记的列表(如果这是您想要的)。

Simplify to this ...

        String l;
        while ((l = inputStream.readLine()) != null) {
            tokens.addAll(Arrays.asList(l.split(" ")));
        }

... creates a list of all tokens on all lines in the file (if that is what you want).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文