誰能告訴我爲什麼我得到一個「NumberFormatException」錯誤? 我的程序要求我輸入10個整數,然後寫入一個文件,並可以一起回讀和平均。NumberFormatException(Java)
我可以輸入並寫入文件就好了,但我得到這個錯誤:在線程
異常「主要」 java.lang.NumberFormatException:對於輸入字符串:「輸入整數:1」 是java .lang.NumberFormatException.forInputString(Unknown Source) at java.lang.Integer.parseInt(Unknown Source) at java.lang.Integer.parseInt(Unknown Source) at average.Average.readRecords(Average.java:99) at average.Average.main(Average.java:29)
for (int i = 0 ; i < 10 ; i++) {
System.out.printf("Please enter integer %d: ", i+1);
numbers[i] = input.nextInt();
{
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 (NumberFormatException numberFormatException)
{
System.out.print("Check for numbers.txt and see what ya got!");
}
的完整代碼...
import java.util.Scanner;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.lang.SecurityException;
import java.nio.file.NoSuchFileException;
import java.util.Formatter;
import java.util.FormatterClosedException;
import java.util.NoSuchElementException;
import java.util.InputMismatchException;
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();
// closeFile();
}
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 program!\n");
for (int i = 0 ; i < 10 ; i++) {
System.out.printf("Please enter integer %d: ", i+1);
numbers[i] = input.nextInt();
{
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();
}
catch (NumberFormatException numberFormatException)
{
System.out.print("Check for numbers.txt and see what ya got!");
}
//System.out.print("? ");
}
}
}
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;
while((line = br.readLine())!= null){
i++;
number [i] = Integer.parseInt(line);
System.out.println("number" +i+" = " +number[i]);
}
}
catch (NoSuchFileException noSuchFileException)
{
System.out.print("This file was not created properly and cannot be found.");
}
catch (IllegalStateException illegalStateException)
{
System.out.print("Error reading from file. Terminating");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
爲什麼不只是使用'%d'? – MadProgrammer
請用輸入和輸出以及整個堆棧跟蹤顯示一個實際的控制檯轉錄。我懷疑錯誤是在輸入'1'後的輸入行。 –
你在這裏發佈的內容看起來好像可能會拋出一個'NumberFormatException'。打印一個堆棧跟蹤並仔細檢查它來自哪裏。我的賭注是在'readRecords()',你試圖從'numbers.txt'解析行,但這完全是猜測,直到你填滿你的問題。 – azurefrog