java - 为什么我会陷入无限循环

标签 java file

我刚刚编写了一个程序,该程序应该扫描文件并计算行数、元音和许多其他内容,但当我运行它时,它会进入无限循环。我很确定它与我不太熟悉的 .hasNext().hasNextLine() 方法有关

import java.util.*;
import java.io.*;
/**
 *
 *
 */
public class Wordcount1 {


    public static void main(String[] args) {
        int vowels=0;
        int punctuation=0;
        int sentences=0;
        int words=0;
        int lines=0;
        int alphaNumeric=0;

       try{
       Scanner input = new Scanner(System.in);
       System.out.println("Enter file name: ");

       File file = new File(input.nextLine());
       Scanner fileReader = new Scanner(file);

       while(fileReader.hasNextLine()){
           lines +=1;
       }
       while(fileReader.hasNext()){
           { fileReader.next();
           words +=1;
           }
           String word = fileReader.next();
           for (int i=0; i<word.length();i++){
               char ch= word.charAt(i);
               if(ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u')
                   vowels +=1;
               if((ch=='!'||ch=='.'||ch=='?'))
                   sentences +=1;
               if(Character.isLetterOrDigit(ch))
                   alphaNumeric +=1;
               switch(ch){
                   case ',':
                    punctuation +=1;
                    break;
                   case '[':
                    punctuation +=1;
                    break;
                   case ']':
                    punctuation +=1;
                    break;
                   case ':':
                    punctuation +=1;
                    break;
                   case '`':
                    punctuation +=1;
                    break;
                   case '-':
                    punctuation +=1;
                    break;
                   case '!':
                    punctuation +=1;
                    break;
                   case '_': 
                    punctuation +=1;
                    break;
                   case '(':
                    punctuation +=1;
                    break;
                   case ')':
                    punctuation +=1;
                    break;
                   case '.':
                    punctuation +=1;
                    break;
                   case '?':
                    punctuation +=1;
                    break;
                   case '"':
                    punctuation +=1;
                    break;
                   case ';':
                    punctuation +=1;
                    break;

               }

           }
       }
        System.out.println("The number of words in the file name: " + words);
        System.out.println("The number of lines in the file name: " + lines);
        System.out.println("The number of alphanumeric characters: "
                + "in the file name: " + alphaNumeric);
        System.out.println("The number of sentences in the file name: "
                + sentences);
        System.out.println("The number of vowels in the file name: " + vowels);
        System.out.println("The number of punctuations in the file name: " 
                + punctuation);

       }catch(FileNotFoundException e){
           e.printStackTrace();
       }
    }
}

最佳答案

while(fileReader.hasNextLine()){
    lines +=1;
}

在这里,您反复询问文件“还有更多行吗”,但实际上从未消耗任何行。因此,它不断回答“是的,我还有更多线路”。

关于java - 为什么我会陷入无限循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14823405/

相关文章:

sql-server - 如何使用 sqlcmd 从 SQL Server 将数据导出为 CSV 格式?

java - 如何找到网站的平均加载时间?

java - Android Dagger 2 不在 java/kotlin 项目中生成组件

java - 加密时出现 OutOfMemory 错误

c++ - 从文本文件中提取文件名

java - java 每两行替换一行到上面一行

java - 当项目导出到 JAR 时,newKieSession() 返回 null

java - String.intern() 如何工作

Java ArrayList IndexOutOfBoundsException 索引 : 1, 大小:1

c - 用 C 读取文本文件,将行分成多个变量