简体   繁体   English

尝试读取java中的.txt文件时,无法将FileNotFoundException解析为类型

[英]FileNotFoundException cannot be resolved to a type when trying to read a .txt file in java

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. 我编写了一个程序来计算.txt文件中某个字母的数量,但是我一直收到错误,指出FileNotFoundException无法解析为某个类型。 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 - 你错过了FileNotFoundException的导入,你可以像这样添加它 -

import java.io.FileNotFoundException;

Or, you might prefer to import the entire java.io package with a wildcard 或者,您可能更喜欢使用通配符导入整个java.io

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 尝试从资源文件夹中读取 txt 文件时出现 Java FileNotFoundException - Java FileNotFoundException when trying to read txt file from resources folder 尝试读取文件时获取java.io.FileNotFoundException - Getting java.io.FileNotFoundException when trying to read a file java.io.FileNotFoundException:尝试读取 excel 文件时 - java.io.FileNotFoundException: when trying to read excel file 尝试ipmlement Java代理时无法解析Java.Type - Java.Type cannot be resolved when trying to ipmlement java proxy java.io.FileNotFoundException:read.txt(系统找不到指定的文件) - java.io.FileNotFoundException: read.txt (The system cannot find the file specified)` 尝试使用扫描仪读取a.txt文件,但继续获取FileNotFoundException - Trying to read in a.txt file with a scanner but keep getting FileNotFoundException 尝试读取Java中的txt文件 - Trying to read a txt file in Java 尝试在Java中读取txt文件时出现java.lang.NullPointerException - java.lang.NullPointerException when trying to read txt file in java 我尝试通过命令行读取txt文件时FileNotFoundException - FileNotFoundException when I try to read txt file via Command Line Java:尝试从文本文件读取时获得FileNotFoundException,即使该文件存在 - Java: Getting FileNotFoundException when trying to read from a text file even though the file exists
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM