这是我的代码。我必须在不使用 FileNotFoundException 类的情况下编写它。代码从包含数组信息的文件中读取。我得到这个错误:
F:\FanClub.java:59: error: unreported exception FileNotFoundException; must be caught or declared to be thrown
Scanner inputFile = new Scanner(file);
谢谢
import java.util.Scanner;
import java.util.ArrayList;
import java.io.*;
public class FanClub
{
private static Scanner keyboard = new Scanner(System.in);
public static void main(String[] args) throws IOException
{
final int NUM_FANS = 100;
int numFans = 0;
int[] ages = new int[NUM_FANS];
String[] names = new String[NUM_FANS];
numFans = fillArrays(names, ages, NUM_FANS);
}
public static int fillArrays(String[] names, int[] ages, int NUM_FANS)
{
int counter = 0;
System.out.print("Enter the file name: ");
String fileName = keyboard.nextLine();
File file = new File(fileName);
Scanner inputFile = new Scanner(file);
if (!file.exists())
{
System.out.println("File " + fileName + " not found.");
System.exit(0);
}
int[] numFans = new int[NUM_FANS];
while (inputFile.hasNext() && counter < numFans.length)
{
numFans[counter] = inputFile.nextInt();
counter++;
}
inputFile.close();
return counter;
}
}