簡體   English   中英

如何通過InputFile進行掃描?

[英]How to scan through an InputFile?

目前,我有一個while循環,可讀取名為“ input”的輸入文件。 該輸入文件包含數字,例如“ 1 2 3 4”,這些數字表示與我的問題無關的內容。 要接收數字“ 1”,我使用scanner.nextInt(); 如果這是接收1的正確方法,那么我將困在如何接收其他數字2 3 4上。

while(scanner.hasNext()){

    int firstnum = scanner.nextInt();
    // How do I get the second, third, and fourth number?

    //It is not guaranteed that I will be given exactly 4 numbers, but I will be given at least that many.

只需繼續循環並累積遇到的數字即可,例如,列出:

List<Integer> numbers = new LinkedList<>();
while(scanner.hasNext()) {
    numbers.add(scanner.nextInt());
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM