Search code examples
javatry-catchfstreaminstantiationtry-catch-finally

How to correctly use try-catch-finally blocks in Java?


What would be the proper way to use the finally block to close out of the file that I was in : event.dat. If you could help me makes sense of this, I'd greatly appreciate it. I've literally spent 4 hours moving things around, playing with the code and searching for answers online, but to no avail. Code works great without that portion, but I need to know how it works for future instances. Thank you and have a good day!

I am having issues with this block:

          finally{
              fstream.close();        
          } 

Which is located in the following code:

    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.");      
}

Solution

  • This is how the scanner should work:

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

    Your problem is that you use the close function when the while conclusion is true. So you should put it outside of the while loop