Write a java program that reads an integer N (2 <= N <= 10000) from an input fil
ID: 3709963 • Letter: W
Question
Write a java program that reads an integer N (2 <= N <= 10000) from an input file named “input7.txt” and then outputsa. All numbers that are divisible by 3 to the output file named “output7A.txt”
b. All primes from 2 to N to the output file named “output7B.txt”
Requirements:
+ For each part a) and b), define a method called readDataFromFile() that takes into account the input filename as the parameter and returns the number N.
+ In part a), write a function called printNumbersToFile() that takes the output filename as the parameter and prints out all numbers which are divisible by 3 (0 excluded).
+ In part b), write a function called printPrimesToFile() that takes the output filename as the parameter and prints out all primes.
Example: Input File “input7.txt” 10 Output File “output7A.txt” 369 File “output7B.txt” 2357
Write a java program that reads an integer N (2 <= N <= 10000) from an input file named “input7.txt” and then outputs
a. All numbers that are divisible by 3 to the output file named “output7A.txt”
b. All primes from 2 to N to the output file named “output7B.txt”
Requirements:
+ For each part a) and b), define a method called readDataFromFile() that takes into account the input filename as the parameter and returns the number N.
+ In part a), write a function called printNumbersToFile() that takes the output filename as the parameter and prints out all numbers which are divisible by 3 (0 excluded).
+ In part b), write a function called printPrimesToFile() that takes the output filename as the parameter and prints out all primes.
Example: Input File “input7.txt” 10 Output File “output7A.txt” 369 File “output7B.txt” 2357
Write a java program that reads an integer N (2 <= N <= 10000) from an input file named “input7.txt” and then outputs
a. All numbers that are divisible by 3 to the output file named “output7A.txt”
b. All primes from 2 to N to the output file named “output7B.txt”
Requirements:
+ For each part a) and b), define a method called readDataFromFile() that takes into account the input filename as the parameter and returns the number N.
+ In part a), write a function called printNumbersToFile() that takes the output filename as the parameter and prints out all numbers which are divisible by 3 (0 excluded).
+ In part b), write a function called printPrimesToFile() that takes the output filename as the parameter and prints out all primes.
Example: Input File “input7.txt” 10 Output File “output7A.txt” 369 File “output7B.txt” 2357 Input File “input7.txt” 10 Output File “output7A.txt” 369 File “output7B.txt” 2357
Explanation / Answer
Below is the solution:
package chegg;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;
public class PrimeNumberFromFile {
public static void main(String[] args) throws FileNotFoundException,IOException, IndexOutOfBoundsException {
String fileName = "D:/input7A.txt"; //file that has to read
Scanner inputFile = new Scanner(System.in); //for file input
inputFile = new Scanner(new File(fileName)); //take a file
int counter =0; //for count the array element
//keep track of how many integers in the file
while(inputFile.hasNextInt())
{
counter++; //count the array element
inputFile.nextInt();
}
Scanner scan2 = new Scanner(new File(fileName));
int a[] = new int[counter]; //create a array with counter
int b[]; //create a array with counter
for(int i=0;i<counter;i++)
{
a[i]=scan2.nextInt(); //fill the array with the integers
//System.out.println(a[i]);
}
PrintWriter writer1 = new PrintWriter("D:/output7A.txt", "UTF-8"); //create a file
for(int j=0;j<counter;j++){
if(a[j]>=2 && a[j]<=1000)
{
if(a[j]%3==0){
writer1.println(a[j]); //write into file
}
}
}
writer1.close(); //close the file output7A file
int count=0;
PrintWriter writer2 = new PrintWriter("D:/output7B.txt", "UTF-8"); //create a file
for(int k=0; k<=counter; k++)
{
if(a[k]>=2 && a[k]<=1000){
count = 0;
for(int j=2; j<a[k]; j++)
{
if(a[k]%j == 0)
{
count++;
break;
}
}
if(count == 0)
{
writer2.println(a[k]);//write into file
System.out.println(a[k]); //print the prime number
}
}
}
writer2.close();
}
}
sample input file:
input7a.txt
12
60
58
47
3
9
13
25
sample output::
output7A.txt:
12
60
3
9
output7B.txt:
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.