Survey
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
Objectives:
Understand what a file is.
Learn how to use the File class
Learn how to use the Scanner class
to read from files
Learn about exceptions and how to
use a try-catch block.
Lab 04-2
Files
A file is a collection of data in mass
storage.
A data file is not a part of a program’s
source code.
The same file can be read or modified by
different programs.
The program must be aware of the format
of the data in the file.
Lab 04-3
Text Files
A text file contains lines of text (e.g., in
ASCII code).
Each line terminates with a “newline”
character (or a combination, carriage
return plus line feed).
Lab 04-4
Text Files
Examples:
– Any plain-text file, typically named
something.txt
– Source code of programs in any
language (e.g., Something.java)
– HTML documents
– Data files for certain programs, (e.g.,
fish.dat;)
Lab 04-5
File Class
An abstract representation of file and
directory pathnames. Requires a String
(file name) as an argument.
File file = new File(“data.dat”);
“Throws” checked exceptions when
anything goes wrong (e.g., the file
doesn’t exist).
FileNotFoundException
Lab 04-6
Scanner
Can read from a file if a File object is
passed as an argument.
Throw a NoSuchElementException if the end
of file has been reached (No more tokens
are available).
import java.util.*;
import java.io.*;
..............................
Scanner reader = new Scanner(new File(fileName));
Lab 04-7
Scanner
By default next(), nextInt(), and nextDouble()
read until they encounter white space. The
white space is the delimiter. A delimiter is a
character or sequence of characters marking
the beginning or end of a unit of data.
The useDelimiter(“pattern”) method of the
Scanner class allows the programmer to
change the delimiter to a specified value.
Scanner reader = new Scanner(new File(inFileName));
reader.useDelimiter(“/”); // changes the delimiter to a /
Lab 04-8
try-catch
try-catch statement should be used to handle
code that throws checked exceptions.
try
{
Scanner reader = new Scanner(new File(“data.dat”));
int i = reader.nextInt();
}
catch (FileNotFoundException e)
{
System.out.println(e);
System.exit(0);
}
Lab 04-9
Top-Down Programming
WAP that calculates the profit made
from selling a specified number of
items. The number of items sold, item
description, unit of measurement, cost,
and the sale price will be read from a
data file “inventory1.dat”.
Lab 04-11
Top-Down Programming
Contents of “inventory1.dat”
6
chocolate milk
gals
2.19
2.99
Lab 04-12
Top-Down Programming (cont’d)
What fields are required to solve the
problem?
NOTE: Fields should be the values we
will read from the data file and
the values we are solving for.
Lab 04-13
Top-Down Programming
6
chocolate milk
gals
2.19
2.99
int quantity
String description
String unit
double cost
double price
double profit
Lab 04-14
Top-Down Programming (cont’d)
Start JCreator.
Open the file called “Lab04.java”.
Lab06.java is in your Lab04 folder.
Lab 04-15
Top-Down Programming (cont’d)
import java.util.Scanner;
import java.io.*;
public class Lab04
{
}
Lab 04-16
Top-Down Programming (cont’d)
The “big problem” is defined in the main
method.
Lab 04-17
Top-Down Programming (cont’d)
public static void main(String[] args)
{
Lab04 lab = new Lab04( );
lab.input();
// Read data from a file
lab.process( ); // Calculate total profit
lab.output( ); // Display output
}
Lab 04-18
Top-Down Programming (cont’d)
In the planning phase we determined that
we need six instance fields to solve the
problem.
Lab 04-19
Top-Down Programming (cont’d)
public class Lab04
{
private int quantity;
private String description;
private String unit;
private double cost;
private double price;
private double profit;
Values to be read
from the data file
Value to be calculated
public static void main(String[ ] args)
{
Lab 04-20
Top-Down Programming (cont’d)
output( )
Lab 04-21
Top-Down Programming (cont’d)
public void output()
{
}
Lab 04-22
Top-Down Programming (cont’d)
public void output()
{
DecimalFormat df = new DecimalFormat(“,###.00”);
}
Lab 04-23
Top-Down Programming (cont’d)
public void output()
{
DecimalFormat df = new DecimalFormat(“,###.00”);
System.out.println("The sale of " + quantity + “ ” +
unit + " of " + description +
" resulted in a profit of " +
df.format(profit )+ “.”);
}
Lab 04-24
Top-Down Programming (cont’d)
process( )
Lab 04-25
Top-Down Programming (cont’d)
public void process()
{
}
Lab 04-26
Top-Down Programming (cont’d)
public void process()
{
profit = quantity * (price - cost);
}
Lab 04-27
Top-Down Programming (cont’d)
input( )
Lab 04-28
Top-Down Programming (cont’d)
Contents of “inventory1.dat”:
6
chocolate milk
gals
2.19
2.99
Lab 04-29
Top-Down Programming (cont’d)
public void input()
{
Reading from a data file requires
a try…catch block.
}
Lab 04-30
Top-Down Programming (cont’d)
public void input()
{
try
{
What we do in the catch block
will always be the same.
}
catch (FileNotFoundException e)
{
}
}
Lab 04-31
Top-Down Programming (cont’d)
catch (FileNotFoundException e)
{
System.out.println(e);
System.exit(0);
}
Lab 04-32
Top-Down Programming (cont’d)
Declare a Scanner object.
try
{
}
Lab 04-33
Top-Down Programming (cont’d)
6
chocolate milk
gals
2.19
2.99
quantity
try
{
Scanner reader = new Scanner(
new File("inventory1.dat"));
}
Lab 04-34
Top-Down Programming (cont’d)
description
6
chocolate milk
gals
2.19
2.99
try
{
Scanner reader = new Scanner(
new File("inventory1.dat"));
quantity = reader.nextInt();
}
Lab 04-35
Top-Down Programming (cont’d)
6
chocolate milk
gals
2.19
2.99
unit
try
{
Scanner reader = new Scanner(
new File("inventory1.dat"));
quantity = reader.nextInt();
description = reader.nextLine();
}
Lab 04-36
Top-Down Programming (cont’d)
6
chocolate milk
gals
2.19
2.99
try
cost
{
Scanner reader = new Scanner(
new File("inventory1.dat"));
quantity = reader.nextInt();
description = reader.nextLine();
unit = reader.next();
// or reader.nextLine()
}
Lab 04-37
Top-Down Programming (cont’d)
6
chocolate milk
gals
2.19
2.99
try
{
price
Scanner reader = new Scanner(
new File("inventory1.dat"));
quantity = reader.nextInt();
description = reader.nextLine();
unit = reader.next();
cost = reader.nextDouble();
}
Lab 04-38
Top-Down Programming (cont’d)
try
{
Scanner reader = new Scanner(
new File("inventory1.dat"));
quantity = reader.nextInt();
description = reader.nextLine();
unit = reader.next();
cost = reader.nextDouble();
price = reader.nextDouble();
}
Lab 04-39
Top-Down Programming (cont’d)
Run The Program:
Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:840)
at java.util.Scanner.next(Scanner.java:1461)
at java.util.Scanner.nextDouble(Scanner.java:2387)
at Lab06.input(Lab06.java:29)
at Lab06.main(Lab06.java:16)
Lab 04-40
Top-Down Programming (cont’d)
What caused the run-time error?
try
{
Scanner reader = new Scanner(
new File("inventory1.dat"));
quantity = reader.nextInt();
nextLine() following
description = reader.nextLine();
nextInt()
unit = reader.next();
cost = reader.nextDouble();
price = reader.nextDouble();
}
Lab 04-41
Top-Down Programming (cont’d)
Clear the
from the input buffer.
try
{
Scanner reader = new Scanner(
new File("inventory1.dat"));
quantity = reader.nextInt();
reader.nextLine();
dummy read
description = reader.nextLine();
unit = reader.next();
cost = reader.nextDouble();
price = reader.nextDouble();
}
Lab 04-42
Top-Down Programming (cont’d)
Run The Program:
The sale of 6 gals of chocolate milk resulted in a profit of
$4.80.
Lab 04-43
Top-Down Programming
What if we change the format of the data file?
Contents of “inventory2.dat”
6/chocolate milk/gals/2.19/2.99
It becomes necessary to change the way the file is read.
Lab 04-44
Top-Down Programming (cont’d)
try
{
Scanner reader = new Scanner(
new File("inventory2.dat"));
reader.useDelimiter(“/”);
change the delimiter
quantity = reader.nextInt();
description = reader.next();
unit = reader.next();
next() not nextLine()
cost = reader.nextDouble();
price = reader.nextDouble();
}
Lab 04-45
Top-Down Programming (cont’d)
Run The Program:
The sale of 6 gals of chocolate milk resulted in a profit of
$4.80.
Lab 04-46
Questions?
Lab 04-47