试图掌握异常处理,但我不确定我是否理解。应该发生的是,如果用户输入整数以外的内容,则不匹配异常应执行并打印该友好消息。另外,关于异常处理的主题,如果我的代码中有任何无用的异常,请告诉我(如果您不介意,请告诉我为什么)。下面链接的完整代码。谢谢!
public static void addRecords() {
System.out.print("Hello, welcome to my magical program!n");
for (int i = 0; i < 10; i++) {
System.out.printf("Please enter integer no. %d: ", i + 1);
numbers[i] = input.nextInt();
System.out.println();
{
try {
output.format("Inputted integer: %s%n", String.valueOf(numbers[i]));
} catch (FormatterClosedException formatterClosedexception) {
System.err.println("Error writing to the file. Terminating.");
break;
} catch (InputMismatchException inputMismatchException) {
System.err.println("Please restart the program and enter integers ONLY.");
break;
} catch (NoSuchElementException elementException) {
System.err.println("Invalid input. Please try again.");
input.nextLine();
}
}
}
}
完整代码在这里:http://pastebin.com/eSGau5ax
我建议从方法中抛出异常并在主方法中捕获它们。这些方法不应决定如何处理异常,而应由调用方(在本例中为 main 方法)决定是否将其打印或记录在文件中。
首先,
java.util.InputMismatchException
不包含整数类型数据的数据文件numbers.txt
发生。请输入相关数据。它将解决此输入不匹配异常。为了更好地理解,请按照教程进行操作:https://examples.javacodegeeks.com/java-basics/exceptions/java-util-inputmismatchexception-how-to-solve-inputmismatchexception/
下一个
使用此代码。它可能会对你有所帮助。
实际上是您在 http://pastebin.com/eSGau5ax 中发布的内容。
有 2 个错误。
第一个错误发生在
try (BufferedReader br = new BufferedReader (new FileReader("numbers.txt"))){
但是我这样更改了它,因为有一些错误。
try{
BufferedReader br = new BufferedReader (new FileReader("numbers.txt"));
另一个错误与输入部分必须在try catch中有关。但是我没有运行你的代码。所有功劳都归于其他SO助手。
numbers[i] = input.nextInt();
现在你的代码看起来像
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Formatter;
import java.util.FormatterClosedException;
import java.util.InputMismatchException;
import java.util.NoSuchElementException;
import java.util.Scanner;
import java.nio.file.NoSuchFileException;
public class Average {
private static Formatter output;
static Scanner input = new Scanner(System.in);
static int[] numbers = new int[10];
public static void main(String[] args) {
openFile();
addRecords();
closeFile();
readRecords();
}
public static void openFile() {
try {
output = new Formatter("numbers.txt");
} catch (SecurityException securityException) {
System.err.println("Write permission denied. Terminating.");
System.exit(1);
} catch (FileNotFoundException fileNotFoundException) {
System.err.println("Error opening file. Terminating.");
System.exit(1);
}
}
public static void addRecords() {
System.out.print("Hello, welcome to my magical program!n");
try {
for (int i = 0; i < 10; i++) {
System.out.printf("Please enter integer no. %d: ", i + 1);
numbers[i] = input.nextInt();
System.out.println();
output.format("Inputted integer: %s%n", String
.valueOf(numbers[i]));
}
} catch (FormatterClosedException formatterClosedexception) {
System.err.println("Error writing to the file. Terminating.");
break;
} catch (InputMismatchException inputMismatchException) {
System.err
.println("Please restart the program and enter integers ONLY.");
break;
} catch (NoSuchElementException elementException) {
System.err.println("Invalid input. Please try again.");
input.nextLine();
}
}
public static void closeFile() {
{
if (output != null)
output.close();
}
}
public static void readRecords() {
try {
BufferedReader br = new BufferedReader(
new FileReader("numbers.txt"));
String line;
int[] number = new int[10];
int i = -1;
int sum = 0;
double average = 0;
while ((line = br.readLine()) != null) {
i++;
String[] split = line.split(":");
line = split[1].trim();
number[i] = Integer.parseInt(line);
System.out.printf("Integer number %d: %d%n", i, numbers[i]);
sum += number[i];
average = (double) sum / 10;
}
System.out.printf("%nWould your sum happen to be %d? %n", sum);
System.out.printf("Which means your average is: %.2f %n", average);
} catch (NoSuchFileException noSuchFileException) {
System.out
.print("This file was not created properly and cannot be found.");
} catch (FileNotFoundException fileNotFoundException) {
System.out
.print("I can't seem to find your file :( That's too bad...");
} catch (IOException ioexception) {
System.out
.print("Whoopsie daisy, you got yourself an IOException. Better luck next time!");
} finally {
System.out
.print("Check your numbers.txt file and see what ya got!");
}
}
}
您正在尝试捕获发生在try
之外的异常。只需在try
内获取下一个Int即可
public static void addRecords() {
System.out.print("Hello, welcome to my magical program!n");
for (int i = 0; i < 10; i++) {
System.out.printf("Please enter integer no. %d: ", i + 1);
System.out.println();
{
try {
numbers[i] = input.nextInt();
output.format("Inputted integer: %s%n", String.valueOf(numbers[i]));
} catch (FormatterClosedException formatterClosedexception) {
System.err.println("Error writing to the file. Terminating.");
break;
} catch (InputMismatchException inputMismatchException) {
System.err.println("Please restart the program and enter integers ONLY.");
break;
} catch (NoSuchElementException elementException) {
System.err.println("Invalid input. Please try again.");
input.nextLine();
}
}
}
}