Question: Java write to file problem Hi all, I am having some trouble with the final part of this question. I'll just let the code and

Java write to file problem

Hi all, I am having some trouble with the final part of this question. I'll just let the code and question speak for itself. It's a lot to try to explain. Essentially, I don't know how to write this particular type of thing to a file. This is part 3 of a 3 part question. The other parts are working correctly. I'm only having trouble writing to the files (bolded below).

Question:

Context: you will extend the program you wrote for the previous exercise, to work with exceptions and basic file I/O.

You need to:

Create a prices.txt file with two double values in two different lines. That is, the context should be something like:

25.50

75.95

Create a text file called hotel.txt that will contain the name of a hotel company. i.e. Hilton Hotel

Modify the printTicket from all three classes so that, instead of printing that message, it returns it. That is, your method now returns the String with the message (Hint: To keep the different lines, add the escape character ).

Modify your Test java class, so that:

In your main method, you are going to create :

One HotelService object. The price will be the first number in the prices.txt file. The name of the hotel company is the value in the hotel.txt file.

One FlightService object. The price will be the second number in the prices.txt file.

IMPORTANT: You need to read the values from the text files. You cannot hard code them in your program.

After you have created the objects, call the printTicket and save the results in two files: airlineTicket.txt, and hotelTicket.txt

Check and handle the following exception:

The exception thrown when a file you want to read or write does not exist.

My code:

import java.util.Scanner; import java.io.*; import java.util.*;

public class ExecuteService3a {

static String[] priceList = new String[2]; static double[] v_priceList = new double[2]; static String[] hotelList = new String[1]; public static void readPrices() throws FileNotFoundException { int lineCounter = 0; File file = new File("prices.txt"); Scanner scanner = new Scanner(file); while (scanner.hasNext()) { String line = scanner.nextLine(); System.out.println(line); double v_line = 0; v_line = Double.parseDouble(line); priceList[lineCounter] = new String(line); v_priceList[lineCounter] = v_line; lineCounter++; } //end while scanner.close(); System.out.println(Arrays.toString(priceList)); System.out.println(Arrays.toString(v_priceList)); } //end readPrices public static void readHotel() throws FileNotFoundException { int lineCounter = 0; File file = new File("hotel.txt"); Scanner scanner = new Scanner(file); while (scanner.hasNext()) { String line = scanner.nextLine(); System.out.println(line); hotelList[lineCounter] = new String(line); lineCounter++; } //end while scanner.close(); System.out.println(Arrays.toString(hotelList)); } //end readHotel

public static void writeHotel() { try { PrintWriter writer = new PrintWriter("hotelTicket.txt"); writer.println(); writer.close(); } catch (FileNotFoundException e) { System.out.println("The file does not exist!"); } //end try catch } //end writeHotel public static void main(String[] args) { try { ExecuteService3a.readPrices(); } catch (FileNotFoundException e) { System.out.println("The file does not exist!"); } //end try catch

try { ExecuteService3a.readHotel(); } catch (FileNotFoundException e) { System.out.println("The file does not exist!"); } //end try catch /* Service3 newService3 = new Service3(50); newService3.getPrice(); newService3.printTicket();*/

HotelService3 newHotelService3 = new HotelService3(v_priceList[0], hotelList[0]); newHotelService3.getPrice(); newHotelService3.printTicket();

FlightService3 newFlightService3 = new FlightService3(v_priceList[1]); newFlightService3.getPrice(); newFlightService3.printTicket(); } //end main } //end ExecuteService3a

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!