如何修复," FoxQZ2.java:39:错误:未报告的异常IOException;必须被抓住或宣布被扔掉#34; jGRASP中出现此错误消息?

时间:2017-09-23 15:51:43

标签: java jgrasp

这是我的代码:说明: 请将您的.java文件命名为:YourLastNameQZ2.java 请开发一个Java程序来计算a中的数字字符数 文本文件(该程序仅生成一行输出)(一个数字字符 在0到9的范围内

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class FoxQZ2 
{
   public static void main(String args[])
   {
      //Initilazing 
      int charCount = 0;
      int wordCount = 0;
      int lineCount = 0;
      BufferedReader reader = null;
      try
      {
         //Reading the text file
         reader = new BufferedReader(new
FileReader("C:/Users/cmf8398/Documents/CIS 201/FoxQZ2.java"));

         //Read lines in file
         String line = reader.readLine();
         while( line != null)
         {
            //Update lineCount every time we read line
            lineCount++;

            //Counting words by space on line
            String[] words = line.split(" ");

            //Update wordCount by how many words in the line
            wordCount = wordCount + words.length;

           //Update charCount by iterating through words on the line
           for(String word: words)
           {
            charCount = charCount + word.length();
           }

           //Read next line
           line = reader.readLine();
         }
         //Output
         System.out.print("Number of Characters in file: "+ charCount);
         System.out.print("Number of Words in file: "+ wordCount);
         System.out.print("Number of lines in file: "+ lineCount);
      }  
      finally
        {
            try
            {
                reader.close();           //Closing the reader
            }
            catch (IOException e) 
            {
                e.printStackTrace();
            }
        }
    } 
}

0 个答案:

没有答案