This program is supposed to do two things:
1) determine if a user input number is prime and if it is, the output will say as much (this works okay)
2) if it is not prime, it should display the number and its prime factors using a set(I'm lost)
Here is the code and pseudocode I have for finding the prime factors. I'm just not understanding what I need to do for getting the prime factors. I'm not looking for free code because I want to understand this as learning
Java has been frustrating for me. Any nudge of assistance may turn the light on for me so I'd appreciate anything you folks can help me with.
Thanks!
<blockquote>
code:
<pre name="code" class="core">
// Program finds the prime factors of a number using sets.
import java.util.Scanner;
import java.util.HashSet;
public class PrimeFactors
{
public PrimeFactors()
{
Scanner scanner = new Scanner( System.in ); // create scanner
System.out.println( "Please enter a number, -1 to terminate:" );
int inputNumber = scanner.nextInt(); // get number
boolean prime = true;
// process user input numbers
while ( inputNumber != -1 )
{
HashSet< Integer > factorSet = new HashSet< Integer >();
factorize( inputNumber, factorSet );
// Now print
for (int i=2; i < inputNumber/2; i++)
{
if (inputNumber%i==0)
prime = false;
}
if (prime)
System.out.println(inputNumber + " is prime");
//If a prime, print the number is a prime
else
System.out.println(inputNumber + " is not prime and its factors are:\n");
//If not a prime, print the prime factors for the number
// Now get the next number
System.out.println("Please enter a number, -1 to terminate:" );
inputNumber = scanner.nextInt(); // get number
} // end while
} // end constructor PrimeFactors
// find prime factors
public boolean factorize( int inputNumber, HashSet< Integer > set )
{
return false;
} // end method factorize
public static void main( String args[] )
{
new PrimeFactors();
} // end main
} // end class PrimeFactors
</pre>
</blockquote>