Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I have two programs in java, one to read from a txt file and one to determine if

ID: 3634029 • Letter: I

Question

I have two programs in java, one to read from a txt file and one to determine if the user input is a prime number. I need the Prime number program to read all the numbers from a txt file and determine which are prime, the output to the console the results. I am having no luck with this, help?



import java.io.*;


public class LookforPrimes {





/**


* @param args the
command line arguments


*/


public static void
main(String[] args)throws Exception {





try{


// Open the file
that is the first


// command line
parameter


FileInputStream
fstream = new FileInputStream("c:/test.txt");


// Get the object
of DataInputStream


DataInputStream in
= new DataInputStream(fstream);


BufferedReader br =
new BufferedReader(new InputStreamReader(in));


String strLine;


//Read File Line By
Line


while ((strLine =
br.readLine()) != null) {


// Print the
content on the console


System.out.println
(strLine);








}


//Close the input
stream


in.close();


}catch (Exception
e){//Catch exception if any



System.err.println("Error: " + e.getMessage());


}





//
FileInputStream fstream = new FileInputStream("c:/test.txt");


// DataInputStream in = new
DataInputStream(fstream);


BufferedReader
bf = new BufferedReader(


new InputStreamReader (System.in));//(System.in));


System.out.println("Enter number to Check:");


// String
strLine;


int num = Integer.parseInt (bf.readLine());


int i;


for (i=2; i < num
;i++ ){


int n = num%i;


if (n==0){



System.out.println("not Prime!");


break;


}


}


if(i == num){



System.out.println("Prime number!");


}


}


}


Explanation / Answer

import java.io.*; public class LookforPrimes { /** * @param args the command line arguments */ public static void main(String[] args)throws Exception { try{ // Open the file //that is the first // command lineparameter FileInputStream fstream = new FileInputStream("c:/test.txt"); // Get the objectof DataInputStream DataInputStream in= new DataInputStream(fstream); BufferedReader br =new BufferedReader(new InputStreamReader(in)); String strLine; //Read File Line ByLine int flag=0; while ((strLine =br.readLine()) != null) { // Print thecontent on the console int temp=Integer.parseInt(strLine); flag=0; for(int i=2;i