Java Reference
In-Depth Information
LISTING 15.5
Continued
34: public static boolean isPrime(int checkNumber) {
35: double root = Math.sqrt(checkNumber);
36: for (int i = 2; i <= root; i++) {
37: if (checkNumber % i == 0)
38: return false;
39: }
40: return true;
41: }
42: }
15
LISTING 15.6
The Full Text of PrimeReader.java
1: import java.io.*;
2:
3: public class PrimeReader {
4: public static void main(String[] arguments) {
5: try {
6: FileInputStream file = new
7: FileInputStream(“400primes.dat”);
8: BufferedInputStream buff = new
9: BufferedInputStream(file);
10: DataInputStream data = new
11: DataInputStream(buff);
12:
13: try {
14: while (true) {
15: int in = data.readInt();
16: System.out.print(in + “ “);
17: }
18: } catch (EOFException eof) {
19: buff.close();
20: }
21: } catch (IOException e) {
22: System.out.println(“Error — “ + e.toString());
23: }
24: }
25: }
Most of the PrimeWriter application is taken up with logic to find the first 400 prime
numbers. After you have an integer array containing the first 400 primes, it is written to a
data output stream in lines 17-31.
 
Search WWH ::




Custom Search