Search code examples
javaswingjcomboboxprintwriter

why doesnt my printWriter work ?


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();
            }





        }

Solution

  • 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?