Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/358.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 当我阅读此文件时,有人能帮我在代码中添加行计数器吗_Java - Fatal编程技术网

Java 当我阅读此文件时,有人能帮我在代码中添加行计数器吗

Java 当我阅读此文件时,有人能帮我在代码中添加行计数器吗,java,Java,我不确定如何添加行计数器,因为如果我执行while语句,例如 while (fileReader.hasNextLine()) { lines+=1; file.nextLine(); } 然后我剩下的元音、句子等都设置为0 我的代码是: Scanner input = new Scanner(System. in ); System.out.println("Enter file name: "); File file = new File(input.nextLine()

我不确定如何添加行计数器,因为如果我执行while语句,例如

while (fileReader.hasNextLine()) {
    lines+=1;
    file.nextLine();
}
然后我剩下的元音、句子等都设置为0

我的代码是:

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

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

if (file.length() == 0) {
    System.out.println("The input file is empty.");
    System.exit(1);
}

Scanner fileReader = new Scanner(file);

while (fileReader.hasNext()) {
    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;

        }
    }
    words += 1;

}
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);
扫描仪输入=新扫描仪(System.in);
System.out.println(“输入文件名:”);
File File=新文件(input.nextLine());
如果(file.length()==0){
System.out.println(“输入文件为空”);
系统出口(1);
}
Scanner fileReader=新扫描仪(文件);
while(fileReader.hasNext()){
String word=fileReader.next();
for(int i=0;i
新行由字符“\n”表示。您可以检查其实例,方法与检查元音、标点符号等相同。

使用这些行集

   String line = fileReader.nextLine();

   for (int i = 0; i < line.length(); i++) {
                 char ch = line.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) {
                    // do something

                 }
             }
             lines ++;
             words += line.split(" ").length;
String line=fileReader.nextLine();
对于(int i=0;i

在您的原始代码中,单词只是行。它们不是这样的单词。

对LineNumberReader有异议吗?您可以在
next
上使用
nextLine
。这需要您解析
String
,但会更容易计算行数-IMHOYou
case
语句可以组合为:
>切换(ch){case',':case'[':case']':/**更多符号**/标点符号+=1;break;}
,您可以将
元音+=1
替换为
+元音
元音+
,以简化操作。移动此条件:
如果(Character.isletorDigit(ch))
到顶部,因为字母也是字母数字,您可以保存一些检查。在这种情况下,标点符号也会切换到
else
。我仍然感到困惑,因为我的循环只考虑单词而不是空格或\n