Discussion in 'OT Technology' started by orangespider, Nov 11, 2004.

1. ### orangespiderThis is the title that appears beneath your name o

Joined:
Jan 13, 2002
Messages:
629
0
output is supposed to look something like:

"Enter choice:
P just to determine if a number is prime.
F to factor the number as well.
Q to quit:
Choice : F
Enter the number : 4
The number 4 factors as follows:
4 = 2 * 2
The number 4 is composite.

Enter choice:
P just to determine if a number is prime.
F to factor the number as well.
Q to quit:
Choice : P
Enter the number : 3
3 is prime.

Enter choice:
P just to determine if a number is prime.
F to factor the number as well.
Q to quit:
Choice : Q

Bye!"

but my factors are not coming out right (or at all) and i do not know how to loop the program so that you can enter as many numbers as you want before hitting Q and "ending" the program. any help would be appreciated. here is my sloppy code that someone already helped me with.

Code:
```import java.util.*;

public class FindPrime2
{
public static void main(String[] args)
{
run();
}

public static void run()
{
String choice = "";
int num = 0;
ArrayList factors;
Scanner scan = new Scanner(System.in);

System.out.println("Enter choice:");
System.out.println("P to just determine if a number is prime.");
System.out.println("F to factor the number as well.");
System.out.println("Q to quit.");
System.out.print("Choice : ");

choice = scan.nextLine();

if (choice.equalsIgnoreCase ("Q"))
System.out.print("Bye!");

if (choice.equalsIgnoreCase ("P"))
{
System.out.print("Enter the number : ");

num = scan.nextInt();

if (Factor.isPrime(num))
System.out.println(num + " is prime.");
else
System.out.println(num + " is not prime.");
}
if (choice.equalsIgnoreCase ("F"))
{
System.out.print("Enter the number : ");
num = scan.nextInt();

factors = Factor.getFactors(num);

if (factors.size() < 1)
System.out.println(num + " is prime.");

else
{
System.out.println("The number " + num + " factors as follows:");
System.out.print(num + " = " + factors.get(0));

for (int count = 1; count < factors.size(); count++)
System.out.print(" * " + factors.get(count));

System.out.println();
System.out.println("The number " + num + " is composite.");
}
}
else
System.out.println("Sorry, Invalid Choice.");

System.out.println();
}
}```

and

Code:
```import java.util.*;

public class Factor
{
public static boolean isPrime(int num)
{
//if there is no factors, isPrime is true
return getFactors(num).size() == 0;
}

//arraylist was only way i find to return factors
public static ArrayList getFactors(int num)
{
ArrayList list = new ArrayList();
int factor;

while(num > 1)
{
factor = 2;
//finds all factors of number by increasing divisor
while (num % factor != 0  && num != factor)
factor++;
//adds each new factor to array list
if (num != factor)
num = num / factor;
}
return list;
}

}```

2. ### RaginBajinHave you punched a donkey today?

Joined:
Dec 24, 2001
Messages:
8,740
0
Location:
NoVA
Well your almost there.. Think about what you are trying to do. You are saying Continue to ask me questions "WHILE" the selection I enter is not Q.

3. ### orangespiderThis is the title that appears beneath your name o

Joined:
Jan 13, 2002
Messages:
629
0

aha! thanks, i didn't realize looping it was that easy. did you get a chance to check how i factored the input? i think there is something wrong. because my output for something like the factorization of 6 is

"The number 6 factors as follows:
6 = 2
The number 6 is composite."

when what i really am trying to say is "6 = 2 * 3"

4. ### korrupshunNew Member

Joined:
Sep 8, 2004
Messages:
389
0
Location:
YourMommasHouse
i would suggest doing a trace of your factor method using the value 6 for num....i think youll find the problem rather quickly

EDIT: better hint=>

think recursively, heres a deeply edited version of a code sample i whipped up

factor (num, list, fact){

if (BASE CASE) return ??;
else if (num % fact == 0) {
return factor ( ??, ??, ??); }
else return factor (??, ??, ??);

}

you should be able to figure it out if youve done the trace and think about this samplette here, let us know how it works out.

Last edited: Nov 11, 2004
5. ### orangespiderThis is the title that appears beneath your name o

Joined:
Jan 13, 2002
Messages:
629
0
hey guys. thanks a ton. i have everything working perfectly now. i'm just having problems "quitting" the program. when i hit q, it says "bye" and then the loop starts again prompting the user for a choice. what am i missing?

6. ### Penguin ManProtect Your Digital Liberties

Joined:
Apr 27, 2002
Messages:
21,696
0
Location:
Edmonton, AB
Post up the code for your loop. Sounds like it's not breaking when it's supposed to.

7. ### sam758OT Supporter

Joined:
Aug 26, 2003
Messages:
901
0
you're missing the break.

if (choice.equalsIgnoreCase ("Q")) {
System.out.print("Bye!");
break;
}

btw. wheres my 20 bucks u promised for the code?

Joined:
Sep 8, 2004
Messages:
389