0% found this document useful (0 votes)
41 views2 pages

Copying and Pasting Java Code

The document discusses copying and pasting Java code from other sources. It provides an example of code that reads employee information from a file. The code uses a Scanner to read various data types from the file, including integers, doubles, and strings. It then demonstrates how someone could follow a similar pattern by copying the basic structure and import statements when reading other file types.

Uploaded by

Ivan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
41 views2 pages

Copying and Pasting Java Code

The document discusses copying and pasting Java code from other sources. It provides an example of code that reads employee information from a file. The code uses a Scanner to read various data types from the file, including integers, doubles, and strings. It then demonstrates how someone could follow a similar pattern by copying the basic structure and import statements when reading other file types.

Uploaded by

Ivan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Copying and Pasting Java Code

By Barry Burd

In Java, like in almost any computer programming language, reading data from a file can be tricky.
You add extra lines of code to tell the computer what to do. Sometimes you can copy and paste
these lines from other peoples’ code.

import java.util.Scanner;
import java.io.File;
import java.io.IOException;
public class DoPayroll {
public static void main(String args[])
throws IOException {
Scanner diskScanner =
new Scanner(new File("EmployeeInfo.txt"));
for (int empNum = 1; empNum <= 3; empNum++) {
payOneEmployee(diskScanner);
}
diskScanner.close();
}
static void payOneEmployee(Scanner aScanner) {
Employee anEmployee = new Employee();
anEmployee.setName(aScanner.nextLine());
anEmployee.setJobTitle(aScanner.nextLine());
anEmployee.cutCheck(aScanner.nextDouble());
aScanner.nextLine();
}
}

For example, you can follow the pattern in this listing:


/*
* The pattern in Listing 8-2
*/
import java.util.Scanner;
import java.io.File;
import java.io.IOException;
class SomeClassName {
public static void main(String args[])
throws IOException {
Scanner scannerName =
new Scanner(new File("SomeFileName"));
//Some code goes here
scannerName.nextInt();
scannerName.nextDouble();
scannerName.next();
scannerName.nextLine();
//Some code goes here
scannerName.close();
}
}

You might also like