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
Click to edit Master title style
•
•
•
•
•
Click to edit Master text styles
Second level
Files
Third level
Fourth level
Section 2.13, Section 8.8
Fifth level
1
Click to edit Files
Master title style
•
•
•
•
••
To use
Click
tofiles,
edit Master
use Filetext
class
styles
Second
File
class
level
can’t read/write
Third
To
read/write
level files, use Scanner/PrintWriter
classes
Fourth level
File
Fifthinput
level can be text-based (what we’ll
do), stream-based
2
Click to edit
Text
Master
Input title style
Use
thetoScanner
classtext styles
• Click
edit Master
•• import
java.io.File;
Secondjava.util.Scanner;
level
•• Put
ThirdScanner
level activity within try { …} catch
(Exception e) {…}
• Fourth level
• Tie object to File object and diskfile name
•• Fifth
level
Use next to get next String, nextInt to get
nextInt, etc. [Must know order of input]
• Close the scanner object.
3
you must put Scanner
declaration in trycatch
Click to edit
Text
Master
Input title style
you must have
new File.
try
{
Scanner s = new Scanner
(new File("filename.txt"));
while (s.hasNext( )) // read to EOF
means input order is
{
String firstS = s.next( );
String, int, double for
int secondI = s.nextInt( );
each record
double thirdD = s.nextDouble( );
// do something with what you just read
}
}
catch (Exception except)
{
// do what happens when things go tragically awry
System.out.println("Things went tragically awry");
System.exit(1);
}
•
•
•
•
•
Click to edit Master text styles
Second level
Third level
Fourth level
Fifth level
}
4
Click to edit
TextMaster
outputtitle style
• Click
to PrintWriter
edit Master text styles
Use
the
class.
••
••
•
•
Second
level
Import java.io.PrintWriter
Third
level
Put inside
Fourth level
try { …} catch (Exception e) {…}
Fifth level
• Tie PrintWriter object to diskfile name
• Use print, println to write to the object
• Close the FileWriter.
5
Click to edit
TextMaster
outputtitle style
try {
•
•
•
•
•
PrintWriter
writer
= new text
PrintWriter("name
of file");
Click
to edit
Master
styles
while(there is more text to write)
Second
level
{
Third ...
level
writer.println(next piece of text);
Fourth
level
...
}
Fifth
level
writer.close();
}
catch(IOException e) {
/* something went wrong with accessing the
file */
}
6
WriteClick
a program
count number
of
to edittoMaster
title style
lines in a file and write the number
• Click to edit
Master
text file
styles
to an
output
• Second level
java countlines myinfile.txt myoutfile.txt
• Third level
would return 2 if file is:
• Fourth level
This is a file
• Fifth
level
with two lines.
myinfile.txt
7
import java.io.*;
Click
to
edit
Master
title
style
public class countlines
{
•// allClick
to edit Master text styles
methods that do something inserted here
• Second level
public static void main(String[] args)
• {Third level
if (args.length != 2)
• Fourth
level
{
• FifthSystem.out.println
level
("to run, type: 'java countlines filein fileout'");
System.exit(1);
}
// create a new countline object with first arg as input file, second as output
countlines cl = new countlines(args[0], args[1]);
}
}
8
// constructor
public countlines(String filein, String fileout)
{
int nbrLines = readfrom(filein);
writeTo(fileout,
• Click
to edit nbrLines);
Master text styles
}
Click to edit Master title style
• Second level
//
method
• write
Third
level
public void writeTo(String fileout, int nbrLines)
•{ Fourth level
outFile = new PrintWriter(new File(fileout));
• PrintWriter
Fifth level
fileout.println("The number of lines is " + nbrLines);
fileout.close( );
}
9
Scanner
Click to Class:
edit Master
for reading
title style
files
public int readfrom(String filein)
{
Scanner scanner;
try {
scanner = new Scanner(new File(filein));
while (scanner.hasNext)
{
String line = scanner.nextLine( ); // also next( ), nextInt( ), nextDouble( )
System.out.println(line); // for debugging only;
countlines++;
}
return countlines;
}
catch (FileNotFoundException ex) {
System.out.println("File not found ...aborting program.");
System.exit(1); }
}
}
•
•
•
•
•
Click to edit Master text styles
Second level
Third level
Fourth level
Fifth level
10
Console input: Section 2.13
ClickScanner
to edit Master
title5style
class: Java
import java.util.*;
class
MyScanner
{
• Click to edit Master text styles
public MyScanner ( )
• { Second level
scanner = new Scanner(System.in);
• Scanner
Third level
System.out.println("Enter a number: ");
• Fourth level
int x = scanner.nextInt();
• Fifth
int y =level
scanner.nextInt();
System.out.println("The sum of " + x +
" + " + y + " is " + (x+y));
}
}
11
File input using Scanner
Click to edit Master title style
import java.util.*;
import java.io.*;
class MyScanner {
public MyScanner ( )
•{ Second level
Scanner scanner;
try {
• Third
level
scanner = new Scanner(new File("infile.java"));
int x = scanner.nextInt( );
int y = scanner.nextInt( );
• Fifth
level
System.out.println("The
sum of " + x +
" + " + y + " is " + (x+y)); }
catch (FileNotFoundException ex) {
System.out.println("File not found ...aborting program.");
System.exit(1); }
}
}
• Click to edit Master text styles
• Fourth level
12
Click
Write
to edit
a Payroll
Master
Program
title style
Write a program that reads in a file in the form first last hourlyrate hoursworked
e.g,:to edit Master text styles
•Susieexempt,
Click
Jones
13.50
40 N
Johnny Jacob
32.30
60 Y
Jane White 35.00 55 N
• Second level
And writes out information to an output file in the form:
first last weeklypay, e.g.,
•Susie
Third
level
Jones: 540.0
Johnny Jacob: 1292.0
Jane White: 2187.5
• Fourth level
Your program should get the filein and fileout names from the command
• line.
FifthPrint
level
the total payroll for the week in a terminal window.
Pay should be figured in the following way: if the employee has worked
no overtime, pay is number of hours * hourly rate. Otherwise, if the
employee is not exempt, the pay is hourly rate * 40 + hourly rate *
1.5 * hours over 40. If the employee is exempt, the employee
receives no overtime (so is paid for no more than 40 hours, less if
the employee worked less).
13