Question

In: Computer Science

Java Programming Project 6: File I/O Purpose: To practice reading from as well as writing to...

Java Programming Project 6: File I/O

Purpose: To practice reading from as well as writing to text files with the help of Scanner class methods, and PrintStream class methods. You will also learn to implement some simple Exception Handling.

Carefully examine and follow ALL the program specifications.

Take a look at the PPT slides for Chapter 7 File I/O for examples that will help with this program.

Hotel Expense Recording Keeping:

A hotel bookkeeper enters client hotel expenses in a text file. Each line contains the following, separated by semicolons: client name, service sold (i.e., Dinner, Conference, Lodging, etc.), the sales amount, and the date.

  1. Your program should first query the user for the name of the input file and read it in. Display an error if the input file is not found (does not exist).
  2. Then the program should read the file (line by line), keep a running tally of the total amount for each kind of service,
  3. Finally display the total amount for each service category. In addition to displaying totals on the screen, the totals should also be written to an output file.
  4. Additionally your program should include some exception handling. An Exception that should be checked (and handled) would be a FileNotFoundException.

Attached (and below) is an example input file that your program will be tested with, so you will need to make sure that you program will run correctly using this file. Since this may be your first experience reading from an input file, you will likely find it easiest if you store the input file in the same folder with your Java program file so that they can easily communicate with one another. The easiest way to store this file is as a plain text file in Notepad (do not use MS word or any other sophisticated word processor or you will be processing embedded text commands, which is not at all recommended). Here is what the input file looks like:

Jason Inouye;Conference;250.00;11/10/2016

Jason Inouye;Lodging;78.95;11/10/2016

Mary Ryan;Dinner;16.95;11/10/2016

Mark Twain;Dinner;25.50;11/10/2016

Mark Twain;Spa;50.00;11/10/2016

Steven Hawking;Conference;250.00;11/10/2016

Steven Hawking;Room Service;45.00;11/11/2016

Steven Hawking;Lodging;78.95;11/11/2016

Ayrton Senna;Room Service;23.20;11/10/2016

Ayton Senna;Dinner;22.50;11/10/2016

Ayton Senna;Lodging;78.95;11/10/2016

One feature of the input file, is that it uses a semicolon (;) to delimit the tokens on each line of input, rather than whitespace. You will need to use a delimiter statement after you construct your line scanner object.

To see how to construct a line scanner object, go to Chapter 7 PowerPoint slide in the Week 13 folder. So for example, if you create an object called lineScan of type Scanner to process tokens on a given line of input, then you could call the useDelimiter method on your lineScan object, as follows:

lineScan.useDelimiter(";");

This will allow you to tokenize each input line based, not on white space delimiters, but using the semicolon as a delimiter instead.

This is what should be in your Output file after you run your program (this file will most likely be located in the same folder as your Java program).

Dinner expenses : 64.95
Lodging expenses : 236.85
Conference expenses : 500.00
Room Service expenses : 68.20
Spa expenses : 50.00

Submission Requirements:

  1. Please be sure to use appropriate prologue comments at the top of your program, Javadoc formatted comments for your methods, as well as inline comments within the body of your code (as needed).

Solutions

Expert Solution

Code:-

import java.util.*;

import java.io.*;

public class Main

{

public static void main(String[] args) throws FileNotFoundException

{

String infile;//input file name

String outfile;//output file name

Scanner sc = new Scanner(System.in);

System.out.print("Enter input file name: ");

infile=sc.nextLine();

Scanner inputScanner=new Scanner(new FileReader(infile));

//do all the computations here

inputScanner.useDelimiter(";");

double conf=0;//conference expense

double lodge=0;//logding expense

double dinner=0;//dinner expense

double spa=0;//spa expenses

double room=0;//room service expenses

while(inputScanner.hasNextLine())//taking input till there exists a line

{

String[] temp= inputScanner.nextLine().split(";");//splitting the input according to delimiter

double x=Double.parseDouble(temp[2]);

if(temp[1].charAt(0)=='C')

{

conf=conf+x;

}

else if(temp[1].charAt(0)=='S')

{

spa=spa+x;

}

else if(temp[1].charAt(0)=='D')

{

dinner=dinner+x;

}

else if(temp[1].charAt(0)=='L')

{

lodge=lodge+x;

}

else

{

room=room+x;

}

}

System.out.printf("Enter output file name: ");

outfile=sc.nextLine();

PrintStream ps = null;//defining the printstream

try//handling exception

{

ps=new PrintStream(outfile);

}

catch (Exception e)

{

System.out.println("Could not open file!");

}

//writing the answer to the output file

ps.print("Dinner Expenses: ");

ps.println(dinner);

ps.print("Lodging Expenses: ");

ps.println(lodge);

ps.print("Conference Expenses: ");

ps.println(conf);

ps.print("Room Service Expenses: ");

ps.println(room);

ps.print("Spa Expenses: ");

ps.println(spa);

return;

}

}

Snippets:-


Related Solutions

I/O Lab Java Purpose To practice the input and output concepts discussed in this module. Specifically,...
I/O Lab Java Purpose To practice the input and output concepts discussed in this module. Specifically, reading from and writing to local files, and formatting output. Instructions Read in file input.csv and generate a cleanly formatted table in output.txt. See the samples below. input.csv name,ID,salary,years experience foo,1,13890,12 bar,2,2342,3 baz,3,99999,24 output.txt Name | ID | Salary | Years experience -----+----+--------+----------------- Foo | 1 | 13890 | 12 Bar | 2 | 2342 | 3 Baz | 3 | 99999 | 24
#Java I am reading from the txt file and I put everytihng inside of the String[],...
#Java I am reading from the txt file and I put everytihng inside of the String[], like that: String[] values = str.split(", "); But, now I have to retrieve the data from it one by one. How can I do it? Here is the txt file: OneTime, finish the project, 2020-10-15 Monthly, wash the car, 2020-11-10 Thanks!
What is the purpose of the XDC file? Simulate the behavioral code Map the I/O from...
What is the purpose of the XDC file? Simulate the behavioral code Map the I/O from VHDL to Basys3 Board Edit the Basys3 Board device type Run the synthesis What is the purpose of declaring a “signal”? Which file contains the list of input combinations used to simulate a circuit in Xilinx Vivado?   XDC Design Testbench What is the purpose of using ieee.std_logic_unsigned.alL or ieee.std_logic_signed.all in lab4 and lab6 testbench2?
Program in Java using Inheritence The purpose of this assignment is to practice OOP programming covering...
Program in Java using Inheritence The purpose of this assignment is to practice OOP programming covering Inheritance. Core Level Requirements (up to 6 marks) The scenario for this assignment is to design an online shopping system for a local supermarket (e.g., Europa Foods Supermarket or Wang Long Oriental Supermarket). The assignment is mostly concentrated on the product registration system. Design and draw a UML diagram, and write the code for the following classes: The first product category is a fresh...
JAVA CODE Learning objectives; File I/O practice, exceptions, binary search, recursion. Design and implement a recursive...
JAVA CODE Learning objectives; File I/O practice, exceptions, binary search, recursion. Design and implement a recursive version of a binary search.  Instead of using a loop to repeatedly check for the target value, use calls to a recursive method to check one value at a time.  If the value is not the target, refine the search space and call the method again.  The name to search for is entered by the user, as is the indexes that define the range of viable candidates...
JAVA PROGRAMMING Objectives JAVA Practice incremental development. Practice getting input from and printing output for the...
JAVA PROGRAMMING Objectives JAVA Practice incremental development. Practice getting input from and printing output for the user. Write a program with conditional statements. Write a program with loops. Specifications Think of three countries you’d like to travel to and look up the conversion rate between dollars and the money used in those countries. (See http://www.ratesfx.com/ rates/rate-converter.html for conversion rates.) Write a program that will convert money from dollars into each of those three types of currency, or from one of...
This week we really want to learn about a file I/O in Java. In Java: 1)...
This week we really want to learn about a file I/O in Java. In Java: 1) Create a plain empty text file named contacts. 2) Populate the text file with a person's name and account number on each line(Joe Doe 123456789). Create 10 accounts. 3) Store that file in the same location as your project 4) Write a program that will find that file and load it into the computers memory. 5) Read each line of the file and print...
in java File encryption is the science of writing the contents of a file in a...
in java File encryption is the science of writing the contents of a file in a secret code. Write an encryption program that works like a filter, reading the contents of one file, modifying the data into a code, and then writing the coded contents out to a second file. The second file will be a version of the first file, but written in a secret code. Although there are complex encryption techniques, you should come up with a simple...
Write a Python program that uses function(s) for writing to and reading from a file: a....
Write a Python program that uses function(s) for writing to and reading from a file: a. Random Number File Writer Function Write a function that writes a series of random numbers to a file called "random.txt". Each random number should be in the range of 1 through 500. The function should take an argument that tells it how many random numbers to write to the file. b. Random Number File Reader Function Write another function that reads the random numbers...
5) File I/O A) Compare and contrast InputStream/OutputStream based file I/O to Scanner/Printwriter based file I/O:...
5) File I/O A) Compare and contrast InputStream/OutputStream based file I/O to Scanner/Printwriter based file I/O: B) Discuss Serialization, what is it? What are the processes and features? What is the function of the keyword Transient?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT