Why is it not possible to call the scanEverything method twice? It only prints out the arrayList once, and the second println shows an empty list

huangapple go评论71阅读模式
英文:

Why is it not possible to call the scanEverything method twice? It only prints out the arrayList once, and the second println shows an empty list

问题

我在同一文件夹中有一个名为test1.txt的文件,举个例子,它包含以下数据:Hello Hello Hello
我的代码只打印一次。我调用了两次这个方法,但第二个println显示了一个空的arraylist。

运行:

[hello,hello,hello]
[]
构建成功(总耗时:0秒)

代码:

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

public class Text {

    public static void main(String[] args) throws FileNotFoundException {
      
            Scanner keyboard = new Scanner(System.in);

            String firstFileName = "test1.txt";
            Scanner scan1 = new Scanner(new File(firstFileName));

            
            System.out.println(scanEverything(scan1));
            System.out.println(scanEverything(scan1));
    }

    public static ArrayList<String> scanEverything(Scanner scan) {
        ArrayList<String> text = new ArrayList<>();
        while (scan.hasNext()) {
            String nextWord = scan.next().toLowerCase();
            text.add(nextWord);
        }
        Collections.sort(text);
        return text;
    }
}
英文:

I have a test1.txt file in the same folder as the rest of the files. Say, for instance, it has the following data: Hello Hello Hello
My code only prints this once. I called the method twice, but the second println shows an empty arraylist.

run:

[hello, hello, hello]
[]
BUILD SUCCESSFUL (total time: 0 seconds)

Code:

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

public class Text {

    public static void main(String[] args) throws FileNotFoundException {
      
            Scanner keyboard = new Scanner(System.in);

            String firstFileName = &quot;test1.txt&quot;;
            Scanner scan1 = new Scanner(new File(firstFileName));

            
            System.out.println(scanEverything(scan1));
            System.out.println(scanEverything(scan1));
    }

    public static ArrayList&lt;String&gt; scanEverything(Scanner scan) {
        ArrayList&lt;String&gt; text = new ArrayList&lt;&gt;();
        while (scan.hasNext()) {
            String nextWord = scan.next().toLowerCase();
            text.add(nextWord);
        }
        Collections.sort(text);
        return text;
    }

答案1

得分: 2

在调用scanEverything之后,扫描器被“使用”,也就是说,scan.hasNext()会返回false。

如果你想要再次扫描该文件,你需要重新创建扫描器(详见这里的详情:https://stackoverflow.com/questions/16573790/java-scanner-rewind)。

英文:

After your call to scanEverything, the Scanner is "consumed", i.e. the scan.hasNext() will return false.

If you want to scan the file again, you have to recreate the Scanner (see here for details: https://stackoverflow.com/questions/16573790/java-scanner-rewind)

答案2

得分: 0

扫描器(Scanner)在你定义为**"scan1"之后的第一次函数调用后被使用,你必须使用不同的扫描器或者关闭第一个扫描器"scan1"**并重新初始化它。

例如,

在调用函数scanEverything之后,使用以下代码:

scan1.close();
scan1 = new Scanner(new File(firstFileName));
英文:

Scanner that you defined as "scan1" got consumed after first function call you have to use different Scanner or close the first scanner that is "scan1" and initiate it again.

For eg.

After calling function scanEverything use below lines

scan1.close();
scan1 = new Scanner(new File(firstFileName));

huangapple
  • 本文由 发表于 2020年9月4日 14:44:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/63736124.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定