In: Computer Science
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.
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:
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:-