3

The following code calculates the average of numbers that are stored in a text file. I have added some exception handling for "file not found" errors. I need to know how to add another exception for when the data in the text file is not numeric (or not int). I thought about adding multiple catches. Not sure how though?

import java.util.*;
import java.io.*;

public class NumAvg2 {

    public static void main(String[] args)
    {
    int c = 1;  
    long sum = 0;
    String strFileName;

    strFileName = args[0];

    Scanner scFileData;
    try{
        scFileData = new Scanner (new File(strFileName));

        while (scFileData.hasNext())
        {
            sum = sum + scFileData.nextInt();
            c++;
        }

    scFileData.close();
    System.out.println("Number of integers: " + (c - 1)); 
    System.out.println( "Average = " + (float)sum/(c - 1));

    } catch (FileNotFoundException e) {
        System.out.println("File not found!");
        System.exit(1);
    }

  } 
}   
4

7 回答 7

6

如果需要,可以使用 Java 7 的multi catch

try {
...
}
catch (FileNotFoundException|InputMismatchException ex) {
//deal with exception
}

这比多次捕获要干净一些。如果抛出这些异常中的任何一个,它将被捕获在单个 catch 块中

于 2012-09-26T19:20:52.410 回答
3
...
} catch (FileNotFoundException e) {
    System.out.println("File not found!");
    System.exit(1);
} catch (InputMismatchException e) {
    // Add stuff into your new exception handling block
}
...

您的 IDE 可能没有对此抱怨,因为 InputMismatchException 是一个 RuntimeException(未经检查的异常),而 FileNotFoundException 是一个常规的检查异常。

于 2012-09-26T19:19:46.740 回答
3
} catch (InputMismatchException e) {
    System.out.println("Not integer!");
    System.exit(1);
} catch (FileNotFoundException e) {
    System.out.println("File not found!");
    System.exit(1);
}
于 2012-09-26T19:20:52.537 回答
3

请注意,如果下一个标记不是整数,Scanner.nextInt也会抛出一个。InputMismatchException

文档

抛出: InputMismatchException - 如果下一个标记与 Integer 正则表达式不匹配,或者超出范围

InputMismatchException是 a RuntimeException,因此可以在try/catch块中忽略,但您也可以显式处理它:

try
{
    // ... Scanner.nextInt
}
catch (FileNotFoundException e)
{
    System.out.println("File not found!");
    System.exit(1);
}
catch (InputMismatchException e)
{
    // ... not an int
}
于 2012-09-26T19:18:01.083 回答
2

要捕获多个异常,您可以像这样链接它们:

public class NumAvg2 {

public static void main(String[] args)
{
int c = 1;  
long sum = 0;
String strFileName;

strFileName = args[0];

Scanner scFileData;
try{
    scFileData = new Scanner (new File(strFileName));

    while (scFileData.hasNext())
    {
        sum = sum + scFileData.nextInt();
        c++;
    }

scFileData.close();
System.out.println("Number of integers: " + (c - 1)); 
System.out.println( "Average = " + (float)sum/(c - 1));

} catch (FileNotFoundException e) {
    System.out.println("File not found!");
    System.exit(1);
} catch(InputMismatchException e){
    //handle it
}
} 
}
于 2012-09-26T19:19:17.553 回答
1
} catch (FileNotFoundException | InputMismatchException e) {
    e.printStackTrace();
    System.exit(1);
}
于 2012-09-26T19:20:44.920 回答
1

您在哪里将数据从文件转换为整数?你应该在那里添加一个 try-catch ..

而不是使用下面的 while 循环,您必须在其中捕获 TypeMismatch 的异常:-

    while (scFileData.hasNext()) {
        try {
            sum = sum + scFileData.nextInt();
            c++;
        } catch (InputMismatchException e) {
            System.out.println("Invalid Input");
        }
    }

您可以使用如下变体:-

    while (scFileData.hasNextInt())
    {
        sum = sum + scFileData.nextInt();
         c++;
    }

scanner.hasNextInt()将自动检查是否输入整数..

另外,当您在 try-catch 块之外声明变量时。我建议不要使用大的 try-catch 块。

相反,您可以将file reading语句包围在 try-catch 周围(这将是一个语句)。然后在几个语句之后,您可以再次将您的语句包围scanner.nextInt()在另一个 try-catch 周围。

所以,我会像这样修改你的代码: -

public class NumAvg2 {

public static void main(String[] args)
{
    int c = 1;  
    long sum = 0;
    String strFileName;

    strFileName = args[0];

    Scanner scFileData;

    try{
        scFileData = new Scanner (new File(strFileName));

    } catch (FileNotFoundException e) {
        System.out.println("File not found!");
        System.exit(1);
    }
    while (true) {
        if (scFileData.hasNextInt())
        {
            sum = sum + scFileData.nextInt();
            c++;
            break;
        } else {
            System.out.println("Enter an integr");
        }
    }

    scFileData.close();
    System.out.println("Number of integers: " + (c - 1)); 
    System.out.println( "Average = " + (float)sum/(c - 1));

}
}

这样你的代码变得更干净.. *只是一个建议..

于 2012-09-26T19:18:13.663 回答