I wrote a program to count the number of a certain letter in a .txt file but I keep getting an error that says FileNotFoundException cannot be resolved to a type. Here is my code.
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;
}
}
}
}
You're missing the import for FileNotFoundException , you can add it like so -
import java.io.FileNotFoundException;
Or, you might prefer to import the entire java.io
package with a wildcard
import java.io.*;
Many IDE(s) can search for the import(s) [and/or] auto-complete, simply go to the end of the word FileNotFoundException
and try pressing CTRL-SPACE
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.