NumberFormatException(Java)

时间:2016-03-09 23:25:21

标签: java exception-handling int try-catch numberformatexception

任何人都可以告诉我为什么我会得到一个" NumberFormatException"错误? 我的程序要求我输入10个整数,然后将其写入文件,并且能够被回读并一起平均。

我可以很好地输入和写入文件,但是我收到了这个错误:

线程中的异常" main" java.lang.NumberFormatException:对于输入字符串:"输入的整数:1"     at java.lang.NumberFormatException.forInputString(Unknown Source)     在java.lang.Integer.parseInt(未知来源)     在java.lang.Integer.parseInt(未知来源)     平均.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();
            }
        }
}

2 个答案:

答案 0 :(得分:1)

所以,问题实际上在您的readRecords方法中,您正在阅读Inputted integer: 1,然后尝试将值解析为int,这显然不是&# 39;吨

您需要从文本中提取数字,可能使用类似......

的内容
while ((line = br.readLine()) != null) {
    i++;
    String[] split = line.split(":");
    line = split[1].trim();
    number[i] = Integer.parseInt(line);
    System.out.println("number" + i + " = " + number[i]);
}

答案 1 :(得分:-1)

如果您使用字符串作为输入,也许您必须尝试使用 在尝试解析空格之前,mystring.trim()为空格。