2015-10-06 30 views
1

什麼是正確的方式來使用finally塊來關閉我所在的文件:event.dat。如果你能幫助我理解這一點,我將不勝感激。我從字面上花了4個小時來移動東西,玩代碼並在網上尋找答案,但無濟於事。代碼在沒有這個部分的情況下效果很好,但我需要知道它對未來的實例是如何工作的。謝謝你,有一個美好的一天!如何在Java中正確使用try-catch-finally塊?

我有這一塊的問題:

  finally{ 
       fstream.close();   
      } 

其中位於下面的代碼:

String answer; 
    String letter; 
    String inType = ""; 
    double inAmount = 0; 
    double amount; 

    description(); 
    GironEventClass newInput = new GironEventClass(); 
    try{ 
     File infile = new File("event.dat"); 
     Scanner fstream = new Scanner(infile); 

     System.out.println("File Contents "); 

     while(fstream.hasNext()) 
     { 
      inAmount = fstream.nextInt(); 
      inType = fstream.next(); 

      try{ 
       newInput.donations(inType, inAmount); 

      } 
      catch(IllegalArgumentException a){ 
       System.out.println("Just caught an illegal argument exception. "); 
      } 
      finally{ 
        fstream.close();   
      } 
     } 

     System.out.println("Total Sales: " + newInput.getSale()); 
     System.out.println("Donations: " + newInput.getDonated()); 
     System.out.println("Expenses: " + newInput.getExpenses()); 



    } 

    catch (FileNotFoundException e){ 
     System.out.println("\nEvent.dat could not be opened. "); 
    } 

    do{ 
     System.out.print("Are there any more items to add that were not in the text file? (Type 'Y' or 'N')"); 
     answer = keyboard.next(); 
     if (("Y".equals(answer)) || ("y".equals(answer))) 
     { 
      letter = inLetter(); 
      amount = inAmount(); 

      newInput.donations(letter, amount); 
     } 

    }while (("Y".equals(answer)) || ("y".equals(answer))); 

    newInput.display(); 
} 

public static String inLetter(){ 
    Scanner keyboard = new Scanner(System.in); 
    String result; 
    String resultTwo; 

    System.out.println("T = Tiket Sales"); 
    System.out.println("D = Donations"); 
    System.out.println("E = Expenses"); 
    System.out.print("Please input an identifier "); 
    result = keyboard.nextLine(); 
    resultTwo = result.toUpperCase(); 

    return resultTwo;  
} 

public static double inAmount(){ 
    Scanner keyboard = new Scanner(System.in); 
    double result; 

    System.out.println("Please input an amount "); 
    result = keyboard.nextInt(); 

    if(result <= 0.0){ 
     System.out.print("Please input a positive and non-zero amount "); 
     result = keyboard.nextInt(); 
    } 

    return result; 
} 

public static void description(){ 
    System.out.println("The program will ask you what amount is being spent on what."); 
    System.out.println(" ex: expenses, ticket sales, and profts."); 
    System.out.println("This program will help determine whether the event generated or lost money.");  
} 
+0

您是否收到任何錯誤?如果是這樣,他們是什麼? –

+1

@NikG這是我收到的錯誤: 異常線程 「main」 java.lang.IllegalStateException:掃描儀關閉 \t在java.util.Scanner.ensureOpen(Scanner.java:1070) \t是java。 util.Scanner.hasNext(Scanner.java:1334) \t at gironjavaassn6.GironJavaAssn6.main(GironJavaAssn6.java:35) – Manny

回答

2

這是掃描儀應該是如何工作的:

while scanner has object 
    read them (one object per method's call) 
when objects are done 
    close the reader. 

你的問題是,你使用close函數時,而得出的結論是正確的。所以,你應該把它放在while循環之外

+0

你太棒了!我把finally塊的內容放在while外觀的外面(甚至不需要finally塊),它就起作用了!非常感謝!我非常感謝你的幫助! – Manny

+0

不客氣!現在您可以將答案標記爲已接受;) –

+0

當然Jürgen。這總是讓我感到很簡單。再次感謝您的幫助。 – Manny

-1

您需要聲明try塊外fstream的,否則這將是不可見最後的塊。

File infile = null; 
File infile = null; 
try { 
    infile = new File("event.dat"); 
    fstream = new Scanner(infile); 

    ... 
} catch () { 
    ... 
} finally { 
    // You need to handle exceptions also here 
    infile.close(); 
    fstream.close(); 
} 

您也可以使用新的try with resources語法,並留下jvm關閉您的流。

+0

從字面上看,這是真的,你不必關閉文件。 –

+0

@Afsin沒錯,但明確開發者的意圖總是更好。所以我爲什麼要解釋它。 –

+0

感謝您的回覆Davide,原來我在while循環中有finally塊。因此,如果循環讀取「沒有更多行讀取 - 退出循環」它永遠不會到達finally塊。我把while循環的內容放在while循環之外,並且它工作正常。感謝您的時間! – Manny

2

The Try block嘗試做一些事情。

The Catch block只,一旦發生在try塊中錯誤的執行。

The Finally block(如果被執行和Catch塊,)Try塊之後執行每一次。

你的問題是你試圖關閉while循環內的fstream。

while(fstream.hasNext()) <----- referencing fstream 
     { 
      inAmount = fstream.nextInt(); 
      inType = fstream.next(); 

      try{ 
       newInput.donations(inType, inAmount); 

      } 
      catch(IllegalArgumentException a){ 
       System.out.println("Just caught an illegal argument exception. "); 
      } 
      finally{ 
        fstream.close();  <---- closing said stream 
      } 
     } 

由於您關閉了流,因此while循環應該只執行一次。也就是說,關閉while循環之外的fstream,你的程序應該恢復正常。

您還可以將try塊,這將工作以及內部while循環。

+1

謝謝Psychrom!這非常有用,並有助於糾正我的代碼。小東西總是讓我感動。無論如何,我非常感謝您花時間幫助,謝謝。 – Manny