Read a file that contains integers. Determine if each of those integers is a pri
ID: 3937842 • Letter: R
Question
Read a file that contains integers. Determine if each of those integers is a prime or not. Complete that program, by defining a checkpoints function, that satisfies the following speed: Function checkpoint takes two arguments, called in_file, out_file. Argument in_file specifies the name of the input, file, that contains the integers to be processed. Argument out_file specifies the name of the output file, where the results will be saved. If in_file cannot be opened for reading, the function should print on the screen "Error in opening file for reading" and return. If out_file cannot be opened for writing, the function should print on the screen "Error in opening file for writing" and return. Otherwise, if both in_file and out_file can be opened: for each integer X stored in in_file, the function writes to out_file a line stating either "X is prime" or "X is not prime".Explanation / Answer
/*
Java Programme to read integers and decide prime or not
*/
//Import required packages
import java.util.*;
import java.io.*;
public class CheckPrimes //class creation CheckPrimes
{
public static void main(String[] args) //main function ,execution starts from here
{
int numbers[]=new int[100]; //array to hold readed integers
try
{
File in_file = new File("cpin1.txt"); //file object creation for input
Scanner scanner = new Scanner(in_file); //scanner object to read input data
int i=0;
while(scanner.hasNextInt()) //repeats loop untill final int in file
{
numbers[i]=scanner.nextInt(); // hold readed values in array
//System.out.println(numbers[i]);
i++;
}
}
catch(IOException ioe) //Exception occures when files opening
{
System.out.println("Error Opening in file for reading : " + ioe);
}
try
{
File out_file = new File("cpout4.txt"); //file object creation FOR OUTPUT
// creates the file
out_file.createNewFile();
// creates a FileWriter Object
FileWriter writer = new FileWriter(out_file); //output writer object to write data to file
for(int p=0;p<numbers.length;p++) //loop repeats to check all ints prine or not
{
int i,m=0,flag=0;
int n=numbers[p]; //it is the number to be checked
m=n/2;
for(i=2;i<=m;i++){
if(n%i==0){
writer.write(numbers[p]+" X is Not Prime"+" "); //write to file if number is not prime
flag=1;
break;
}
}
if(flag==0)
writer.write(numbers[p]+"X is Prime"+" "); //write to file if number is prime
writer.flush();
}
// Writes the content to the file
writer.close();
}
catch(IOException ioe) //Exception occures when files opening
{
System.out.println("Error Opening in file for Writing : " + ioe);
}
}
}
Output :
cpin1.txt :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
Cpout.txt;
1X is Prime
2X is Prime
3X is Prime
4 X is Not Prime
5X is Prime
6 X is Not Prime
7X is Prime
8 X is Not Prime
9 X is Not Prime
10 X is Not Prime
11X is Prime
12 X is Not Prime
13X is Prime
14 X is Not Prime
15 X is Not Prime
16 X is Not Prime
17X is Prime
18 X is Not Prime
19X is Prime
20 X is Not Prime
21 X is Not Prime
22 X is Not Prime
23X is Prime
24 X is Not Prime
25 X is Not Prime
26 X is Not Prime
27 X is Not Prime
28 X is Not Prime
29X is Prime
30 X is Not Prime
31X is Prime
32 X is Not Prime
33 X is Not Prime
34 X is Not Prime
35 X is Not Prime
36 X is Not Prime
37X is Prime
38 X is Not Prime
39 X is Not Prime
40 X is Not Prime
41X is Prime
42 X is Not Prime
43X is Prime
44 X is Not Prime
45 X is Not Prime
46 X is Not Prime
47X is Prime
48 X is Not Prime
49 X is Not Prime
50 X is Not Prime
51 X is Not Prime
52 X is Not Prime
53X is Prime
54 X is Not Prime
55 X is Not Prime
56 X is Not Prime
57 X is Not Prime
58 X is Not Prime
59X is Prime
60 X is Not Prime
61X is Prime
62 X is Not Prime
63 X is Not Prime
64 X is Not Prime
65 X is Not Prime
66 X is Not Prime
67X is Prime
68 X is Not Prime
69 X is Not Prime
70 X is Not Prime
71X is Prime
72 X is Not Prime
73X is Prime
74 X is Not Prime
75 X is Not Prime
76 X is Not Prime
77 X is Not Prime
78 X is Not Prime
79X is Prime
80 X is Not Prime
81 X is Not Prime
82 X is Not Prime
83X is Prime
84 X is Not Prime
85 X is Not Prime
86 X is Not Prime
87 X is Not Prime
88 X is Not Prime
89X is Prime
90 X is Not Prime
91 X is Not Prime
92 X is Not Prime
93 X is Not Prime
94 X is Not Prime
95 X is Not Prime
96 X is Not Prime
97X is Prime
98 X is Not Prime
99 X is Not Prime
100 X is Not Prime
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.