1

im not sure why this isnt working, ive tried everything i can on it. I feel like im very close to getting this right but just cant get the last few things to set together. i also realise this is a not very well coded but i am a beginner at this and would appreciate any help i can get.

        public void actionPerformed(ActionEvent event) {
        Object source = event.getSource();
        Object flight = AirlinesBox.getSelectedItem();
        Object DepartureLocation = LocationsBox.getSelectedItem();
        Object Destination = DestinationBox.getSelectedItem();
        Object ReturnOrSingle = TypeOfFlightBox.getSelectedItem();
        Object FlightDate = DateBox.getSelectedItem();
        Object FlightTime = TimeBox.getSelectedItem();
        Object FlightSeat = classOfSeatBox.getSelectedItem();
        Object FlightBag = FlightBagBox.getSelectedItem();
        Object FlightMeal = FlightMealBox.getSelectedItem();



        if(source == PrintTicket)
        {

            try {
                PrintWriter write = new PrintWriter("Receipt.txt");
                write.println("_______________________________________");
                write.println("|                                      |");
                write.println("|   --------------------------------   |");
                write.println("|   |  This is your Flight Receipt |   |");
                write.println("|   |      Please keep it safe     |   |");
                write.println("|   --------------------------------   |");
                write.println("|                                      |");
                write.println("|   --------------------------------   |");
                write.println("|   |                              |   |");
                write.println("     Airline= " + flight);
                write.println("     Departure Location= " + DepartureLocation);
                write.println("     Destination= " + Destination);
                write.println("     Flight Type= " + ReturnOrSingle);
                write.println("     Flight Day= " + FlightDate);
                write.println("     Flight Time= " + FlightTime);
                write.println("     Flight Seat Type= " + FlightSeat);
                write.println("     Extra Bags= " + FlightBag);
                write.println("     Flight Meal= " + FlightMeal);
                write.println("|   |                              |   |");
                write.println("|   --------------------------------   |");
                write.println("----------------------------------------");

                write.close();



                System.out.println(AirlinesBox.getSelectedItem());
                System.out.println(LocationsBox.getSelectedItem());
                System.out.println(DestinationBox.getSelectedItem());
                System.out.println(TypeOfFlightBox.getSelectedItem());
                System.out.println(DateBox.getSelectedItem());
                System.out.println(TimeBox.getSelectedItem());
                System.out.println(classOfSeatBox.getSelectedItem());
                System.out.println(FlightBagBox.getSelectedItem());
                System.out.println(FlightMealBox.getSelectedItem());

                }

            catch (FileNotFoundException e) 
            {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }





        }
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • Post a [Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve) – Reimeus Jul 07 '15 at 16:18
  • Time to do some debugging, since you must first isolate the bug before you can fix it. – Hovercraft Full Of Eels Jul 07 '15 at 16:19
  • this is the minimal ? – UpAllNight123 Jul 07 '15 at 16:19
  • I'll ask the obvious question - What exactly is it that is not working, and what are the errors you are getting? – Sh4d0wsPlyr Jul 07 '15 at 16:20
  • thats is the problem there is not errors when its running it simply doesnt do anything i want it to print the receipt into a new txt file but when the button is pressed it doesnt do anything, i only put the println part in there to make sure that the If statement was working – UpAllNight123 Jul 07 '15 at 16:22
  • Is there a specific reason for using PrintWriter? – Sh4d0wsPlyr Jul 07 '15 at 16:24
  • @UpAllNight123 but it is not complete, so any answer here would be a guess – Reimeus Jul 07 '15 at 16:25
  • isnt that the way to get it to print to a new file ? – UpAllNight123 Jul 07 '15 at 16:26
  • 1
    Did you add this `ActionListener` to the `PrintTicket` component ? Maybe `source == PrintTicket` is never `true` – Titus Jul 07 '15 at 16:29
  • 1
    `"this is the minimal"` -- and completely non-runnable and non-testable. Please read or re-read the link to the first comment. Also, `"thats is the problem there is not errors when its running it simply doesnt do anything i want it to print the receipt into a new txt file but when the button is pressed it doesnt do anything, i only put the println part in there to make sure that the If statement was working"` -- debugging is much more than simply fixing compilation errors. It means using a debugger and following code as it's running to see where the logic errors are. – Hovercraft Full Of Eels Jul 07 '15 at 16:32
  • thanks for the help everyone in the end it was printing a receipt.txt but was sending it somewhere to my computer, as soon as i put a pathway to my desktop it worked perfectly :) so thanks the help – UpAllNight123 Jul 09 '15 at 14:20

1 Answers1

1

PrintWriter has a nasty habit of hiding the errors from you. I would recommend swapping to another read/write class. Please take a look a the following for information on the BufferedWriter. Or if you are stuck with PrintWriter, look into how to check the errors.

http://www.mkyong.com/java/how-to-write-to-file-in-java-bufferedwriter-example/

Be aware, you will have to add newline characters with BufferedReader since it doesn't have a println method. For a little extra detail on the differences check out this post.

Difference between java.io.PrintWriter and java.io.BufferedWriter?

Community
  • 1
  • 1
Sh4d0wsPlyr
  • 948
  • 12
  • 28
  • I'd say `PrintStream` (perhaps wrapping a `BufferedOutputStream`) rather than `BufferedWriter`, since that will let the OP keep the `println` statements, and will have the same use cases as `System.out` (and still throws exceptions as they occur) – Ian McLaird Jul 07 '15 at 16:55