Stuck on Hotel Salesperson Java Program [closed] Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of Stuck on Hotel Salesperson Java Program [closed] without wasting too much if your time.

The question is published on by Tutorial Guruji team.

Here is the problem: A hotel salesperson enters sales in a text file. Each line contains the following, separated by semicolons: The name of the client, the service sold (such as Dinner, Conference, Lodging, and so on), the amount of the sale, and the date of that event. Write a program that reads such a file and displays the total amount for each service category. Display an error if the file does not exist of the format is incorrect.

Here is what I have:

package practice;

import java.util.*;

public class practice1 {

     * @param args the command line arguments
    public static void main(String[] args) throws FileNotFoundException {
    Scanner console = new Scanner(;
    System.out.println("Please enter input file name: ");
    String inputFileName =;
    System.out.println("Please enter desired output file name: ");
    String outputFileName =;

    //Construct Scanner and PrintWriter objects for reading and writing

    File inputFile = new File(inputFileName);
    Scanner in = new Scanner(inputFile);
    PrintWriter out = new PrintWriter(outputFileName);
    double dinnerTotal = 0;
    double conferenceTotal = 0;
    double lodgingTotal = 0;
    double total = dinnerTotal + conferenceTotal + lodgingTotal;

    //Read the input and write the output

    while (in.hasNext())
        String line = in.nextLine();
        String[] parts = line.split(";");
            conferenceTotal = conferenceTotal+ Double.parseDouble(parts[3]);
        } else if(parts[2].equals("Dinner")){
            dinnerTotal += Double.parseDouble(parts[3]);
        } else if(parts[2].equals("Lodging")){
            lodgingTotal += Double.parseDouble(parts[3]);


    out.printf("Dinner Total:", dinnerTotal);
    out.printf("Conference Total:", conferenceTotal);
    out.printf("Lodging Total", lodgingTotal);
    out.printf("Total", total);



This program creates a new file that looks like this: Dinner Total Conference Total Lodging Total Total

It doesn’t print the actual totals though. How do I print the totals?


double total = dinnerTotal + conferenceTotal + lodgingTotal; line needs to go below the while loop.

We are here to answer your question about Stuck on Hotel Salesperson Java Program [closed] - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji