gpt4 book ai didi

java - 尝试在 java 中读取 .txt 文件时 FileNotFoundException 无法解析为类型

转载 作者:行者123 更新时间:2023-12-02 06:20:17 24 4
gpt4 key购买 nike

我编写了一个程序来计算 .txt 文件中某个字母的数量,但我不断收到一条错误,指出 FileNotFoundException 无法解析为某种类型。这是我的代码。

import java.util.Scanner;
import java.io.File;
import java.io.IOException;
import java.io.BufferedReader;

public class Count
{
public static void main (String[] args) throws FileNotFoundException {

String phrase; // a string of characters
int countBlank; // the number of blanks (spaces) in the phrase
int length; // the length of the phrase
char ch; // an individual character in the string
int countA;
int countE;
int countS;
int countT;

java.io.File file = new java.io.File("counting.txt");
Scanner inFile = new Scanner (file);

Scanner scan = new Scanner(System.in);

phrase = scan.nextLine();
length = phrase.length();

// Initialize counts

while (true)
{
if (phrase.equalsIgnoreCase("quit"))

break;

else
{

countBlank = 0;
countA = 0;
countE = 0;
countS = 0;
countT = 0;

for ( int i = 0; i < length; i++ )
{
if ( phrase.charAt( i ) == ' ' )

countBlank++;
ch = phrase.charAt(i);

switch (ch)
{
case 'a':
case 'A': countA++;
break;
case 'e':
case 'E': countE++;
break;
case 's':
case 'S': countS++;
break;
case 't':
case 'T': countT++;
break;
}

}


System.out.println ();
System.out.println ("Number of blank spaces: " + countBlank);
System.out.println ();

System.out.println ("Number of A's: " + countA);
System.out.println ();
System.out.println ("Number of E's: " + countE);
System.out.println ();
System.out.println ("Number of S's: " + countS);
System.out.println ();
System.out.println ("Number of T's: " + countT);
break;
}
}
}
}

最佳答案

您缺少 FileNotFoundException 的导入,你可以像这样添加 -

import java.io.FileNotFoundException;

或者,您可能更愿意使用通配符导入整个 java.io

import java.io.*;

许多 IDE 可以搜索导入[和/或]自动完成,只需转到单词的末尾 FileNotFoundException 并尝试按 CTRL-SPACE .

关于java - 尝试在 java 中读取 .txt 文件时 FileNotFoundException 无法解析为类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21080408/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com