I have the following java script. I would like the result (line 21) to state \"L
ID: 3722406 • Letter: I
Question
I have the following java script. I would like the result (line 21) to state "List of primes" before it prints the primes.
And how can I get the statement (line 22) to print on the next line after the result, when it runs the loop?
3 import java.util.*; 5 public class PrimeFactors public static void main (String args) Scanner reader new Scanner(System. in); System.out.print( Please enter an integer greater than 1 C0 to terminate): " 10 13 14 15 try long str = reader.nextLong(); if (str 1)1 20 21 primes(str); System. out.printlnC"Enter another integer greater than 1 ( to terminate):); 23 else if (str0) System. out.printlnC"Program terminated; break 25 27 28 // Makes sure the program catches strings as an error. Prints error message and clears Scanner catch (InputMi smatchException e) 30 31 32 System. out.println"Input is not an integer. Please enter an integer greater than 1 C0 to terminate): "); reader.nextLineO I while (true); reader.close); 35 37 38 39 public static void primes(long n) / Print the number of 2s that divide n while (r%2-=0) { 41 System.out.print(2+""); 45 46 47 48 //n must be odd at this point. So we can skip one element (Note i- i +2) for (long 1-3; 1 Math.sqrt(n); 1+-2) { //While i divides n, printi and divide n while Cni0) System.out.print(i "); 50 51 52 53 //This condition is to handle the case when the integer is a prime number greater than2 if(n>2) 56 57 System.out.print(n); 59 AL Problems Declaration Console X PrimeFactors (1) [Java Application] /Library/Java/JavaVirtualMachines/jdk-9.0.4.jdk/Contents/Home/bin/java (3. mar. 2018 14.08.54) Please enter an integer greater than 1 (0 to terminate): 9 3 Enter another integer greater than 1 (0 to terminate):Explanation / Answer
Following is the changed code: I changed the primes function code to print the list of primes:
package chegg;
import java.util.InputMismatchException;
import java.util.Scanner;
public class PrimeFactors {
public static void main(String[] args){
Scanner reader = new Scanner(System.in);
System.out.print("Please enter on integer greater than 1 (0 to terminate): ");
do {
try{
long str = reader.nextLong();
if(str<0){
System.out.println(str + "is not an integer greater than 1.Please enter an integer greater than 1 (0 to terminate): ");
}
else if(str>1){
primes(str);
System.out.println("");
System.out.println("Enter another integer greater than 1 (0 to terminate): ");
}
else if(str == 0){
System.out.println("Program terminated");
break;
}
}
catch(InputMismatchException e){
System.out.println("Input is not an integer. Please enter an integer greater than 1 (0 to terminate): ");
reader.nextLine();
}
}while(true);
reader.close();
}
public static void primes(long n){
int j =0;
//Empty String
String primes = "";
for (int i = 1; i <= n; i++)
{
int count=0;
for(j =i; j>=1; j--)
{
if(i%j==0) // i%j == 0 than add the string
{
count = count + 1;
}
}
if (count ==2)
{
//Appended the Prime number to String
primes = primes + i + " ";
}
}
System.out.print(primes);
}
}