2016-03-09 96 views
-2

誰能告訴我爲什麼我得到一個「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(); 
      } 
     } 
} 
+0

爲什麼不只是使用'%d'? – MadProgrammer

+1

請用輸入和輸出以及整個堆棧跟蹤顯示一個實際的控制檯轉錄。我懷疑錯誤是在輸入'1'後的輸入行。 –

+0

你在這裏發佈的內容看起來好像可能會拋出一個'NumberFormatException'。打印一個堆棧跟蹤並仔細檢查它來自哪裏。我的賭注是在'readRecords()',你試圖從'numbers.txt'解析行,但這完全是猜測,直到你填滿你的問題。 – azurefrog

回答

-1

如果您正在使用的輸入字符串,也許你有使用 mystring.trim()爲空白嘗試分析它之前嘗試。

+2

這應該是評論,而不是回答,我知道,你沒有代表,不要濫用系統發表評論作爲答案。這樣做會使得難以得到需要發表評論的代表。 –

1

所以,這個問題實際上是在你的readRecords方法,你讀Inputted integer: 1,然後試圖將值解析到int哪裏,它顯然不是

您需要從提取的數文本,也許使用類似...

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]); 
} 
+0

Ay this work非常好!謝謝你! – Walby

相關問題