2015-11-05 108 views
-2

這是一個修復後表達式計算器和使用文件輸入進行操作的文件讀取器。當按下F並輸入文件名時,程序將假設對文件的內容進行操作,並在輸出有效時輸出,但是它會一直跳到catch並拋出錯誤。我不明白爲什麼,有什麼建議?Java文件未被正確讀取

import java.io.File; 
import java.util.Scanner; 
import java.io.FileNotFoundException; 
public class assignment { 

    public static void main(String[] args) { 
     String option; 
     char letter; 
    while(true) 
    { 
     Scanner scanner = new Scanner(System.in); 
     System.out.println("Enter K to input data or F to use file input"); 
     option = scanner.nextLine(); 
     letter = option.charAt(0); 

     switch (Character.toUpperCase(letter)) 
     { 
     case 'K': 
     System.out.println("Please enter a calculation, or press enter to close"); 

     String line = scanner.nextLine(); 

      if (line.equals("")) 
      { 
       System.out.println("Calculator has been closed"); 
       System.exit(0); 
      } 

     String [] element = line.split(" "); 

      if (element.length == 3) 
      { 
      System.out.println("You enter: " + element[0]+ " " + element[1]+" " + element[2]); 
      try{ 
       double number1, number2; 
       number1 = Double.parseDouble(element[0]); 
       number2 = Double.parseDouble(element[1]); 

        if (element[2].equals("+")) 
        { 
         System.out.println("Total:" + (number1 + number2)); 
        } 
        else if (element[2].equals("/")) 
        { 
         System.out.println("Total:" + (number1/number2)); 
        } 
        else if (element[2].equals("-")) 
        { 
         System.out.println("Total:" + (number1 - number2)); 
        } 
        else if (element[2].equals("*")) 
        { 
         System.out.println("Total:" + (number1 * number2)); 
        } 
        else{ 
        System.out.println("That is incorect input; please try again"); 
        scanner.close(); 
        } 
       } 
      catch(NumberFormatException e) 
       { 
       System.out.println("Error"+e.getMessage()); 
       } 
     } 
      else{ 
       System.out.println("You have inserted Incorrect input"); 
      } 

     break; 
     case 'F': 
      try{ 
      System.out.println("you have chosen to use a file input"); 
      Scanner file = new Scanner (System.in); 
      System.out.println("input file name:"); 
      String input = file.nextLine(); 
      Scanner s = new Scanner (new File (input)); 


       while (s.hasNext()) { 

      line = s.nextLine(); 
      String FileInput [] =input.split(" "); 

       try 
       { 
        double inp1, inp2; 
        inp1 = Double.parseDouble(FileInput[0]); 
        inp2 = Double.parseDouble(FileInput[1]); 

        if (FileInput[2].equals("+")) 
        { 
         System.out.println("Total:" + (inp1 + inp2)); 
        } 
        else if (FileInput[2].equals("/")) 
        { 
         System.out.println("Total:" + (inp1/inp2)); 
        } 
        else if (FileInput[2].equals("-")) 
        { 
         System.out.println("Total:" + (inp1 - inp2)); 
        } 
        else if (FileInput[2].equals("*")) 
        { 
         System.out.println("Total:" + (inp1 * inp2)); 
        } 
        else 
        { 
        System.out.println("That is incorect input; please try again"); 
        } 
       } 
       catch(NumberFormatException e) 
       { 
        System.out.println("invalid number" + e); 
       } 
      } 
      System.out.println("\nEOF"); 
      file.close(); 
      s.close(); 
      break; 
      } 
      catch (FileNotFoundException e) { 
       System.out.println("incorrect filename"); 
      } 
      default: 
    System.out.println("Incorrect letter entered"); 
    }   
} 
} 
} 
+1

最新錯誤?我看到numberformat異常或filenotfound異常。哪一個?也可以嘗試將's.hasNext()'改爲's.hasNextLine()' – 3kings

+2

並且它「拋出」的錯誤是......? (總是報告拋出的實際異常。)如果重新格式化了代碼,它也會有所幫助... –

+1

還要注意,您提供的大部分代碼似乎與問題無關。我的猜測是這個問題實際上是你在System.in上創建了多個掃描器,但目前很難說清楚。 –

回答

1

您應該只創建一次Scanner實例。
廣場 Scanner scanner = new Scanner(System.in); 如在第一條語句的主(循環之前),並刪除其他new Scanner
的原因是每個掃描儀緩衝。