Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Subject: java programming Below is the code for read the file. Please help me to make code for write the data based on THE QUESTION.

Subject: java programming

Below is the code for read the file. Please help me to make code for write the data based on THE QUESTION. Please put array for the data just what i did in the code below. Match it with my code. Thank you.

import java.io.*;

import java.util.Scanner;

public class GuestInfo {

public static void main(String[] args) {

try {

// Open the input file

File file = new File("guest.txt");

Scanner sc = new Scanner(file);

int breakfastCount = 0;

// Read each line of the file

while (sc.hasNextLine()) {

String line = sc.nextLine();

String[] guestInfo = line.split(" ");

// Display the guest information

System.out.println("Room No: " + guestInfo[0]);

System.out.println("Guest Name: " + guestInfo[1] + " " + guestInfo[2]);

System.out.println("Type: " + guestInfo[3]);

System.out.println("Breakfast: " + guestInfo[4]);

System.out.println("Price: " + guestInfo[5]);

System.out.println("Date of Stay: " + guestInfo[6]);

System.out.println();

// Count the number of guests who opt for breakfast

if (guestInfo[4].equals("Yes")) {

breakfastCount++;

}

}

// Display the number of guests who opt for breakfast

System.out.println("Guests with a \"Yes\" for breakfast: " + breakfastCount);

// Close the input file

sc.close();

} catch (FileNotFoundException e) {

System.out.println("Error: Unable to open file.");

}

}

}

image text in transcribed

The following data contains information of guest staying at Hotel B stored in a text file named guest.txt. Each line contains the guest's room no, the guest's name, the type of room, breakfast option, total price for room and date of stay. Write a Java program with a method that does the following: i) Reads the input file guest.txt and display the information onto the screen ii) Count the number of guests who opt "Yes" for breakfast in the file and display the result onto the screen. Input file (guest.txt) 1501 Salina Mohd Sani Deluxe Yes 240 16-17/01/23 812 Abdullah Muhd Ismail Family No 3502425/01/23 1105 Chan Lee Wei Suite Yes 485 23-25/01/23 210 Mohd Firdhaus Hussin Executive Yes 6202224/01/23 608 David Anand Ravi Executive Yes 8451417/02/23 1209 Timothy Lee Anderson Single No 1801819/02/23 Sample Output Guests with a "Yes" for breakfast: 4

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Expert Performance Indexing In SQL Server

Authors: Jason Strate, Grant Fritchey

2nd Edition

1484211189, 9781484211182

More Books

Students also viewed these Databases questions

Question

What is the relationship between SUE and fundamental analysis?

Answered: 1 week ago

Question

Understand why customers are loyal to a particular service firm.

Answered: 1 week ago