试图掌握异常处理,但我还不确定我是否理解。应该发生的事情是,如果用户输入的不是整数,则应该执行mismatchexception并打印该友好消息。此外,关于异常处理的主题,如果我的代码中有任何无用的例外,请告诉我(如果你不介意的话,为什么)。完整代码链接如下。谢谢!
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
答案 0 :(得分:0)
<强>首先,强>
对于不包含整数类型数据的数据文件numbers.txt
,会发生 try (BufferedReader br = new BufferedReader (new FileReader("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"));
但我改变了它,因为有一些错误。
numbers[i] = input.nextInt();
另一个错误与获取输入部分有关必须在try catch中。但是我还没有运行你的代码。所有功劳都归功于其他SO助手。
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!");
}
}
}
现在您的代码看起来像
<ref local="authenticationManager" />
答案 1 :(得分:0)
我建议从方法中抛出异常并在main方法中捕获它们。这些方法不应该决定如何处理异常,而是调用者(在这种情况下是主方法)决定是打印还是将其记录在文件中。
答案 2 :(得分:0)
您正试图捕捉try
之外发生的异常。
只需在try
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();
}
}
}
}