1. Member
Join Date
May 2013
Posts
12
Rep Power
0

Problem with a loop

I'm writing my homework assignment and it's going pretty well but I have one major outlying problem: I can't get it to loop properly.
The error handling and the algorithm is just fine, but this darn loop is going to be the end of me. Whats wrong with this loop?
This code is a little long; The method that needs to loop starts at line 89 I believe.

Java Code:
import java.util.Scanner;
public class A4LoopTest {

static String input;
static int num=0;
static char ch;
public static int PrimeTest (int answer) {

int valid=0;

while(true){
System.out.print("Please enter a positive integer or type q to quit: ");
Scanner scan = new Scanner(System.in);
input = scan.nextLine();

while(valid == 0) {
try {
if(input.equalsIgnoreCase("q")) {

}
Integer.parseInt(input);
break;
} catch(Exception e) {
System.out.print("Not a valid integer!\r");
System.out.print("Please enter a positive integer or type q to quit: ");
input = scan.nextLine();
}

}

int num = Integer.parseInt(input);

for(int i = num - 1; i > 0; i--) {

if(num == 2) {

}

if(num%i==0){

}
else if(i==2){

}
}

}

}

public static int LargestDivisor (int lgdiv){
int i;
int num = Integer.parseInt(input);
for(i=2;i<num;i++){
if (num%i==0){
int LargestDiv=num/i;
lgdiv=LargestDiv;
break;
}
}
return lgdiv;
}

public static int LargestSet (int lset){
int i;
int num = Integer.parseInt(input);
for(i=2;i<num;i++){
if (num%i==0){
int largestSet=i;
lset=largestSet;
break;
}
}
return lset;
}

public static void main(String []args){
Scanner scan=new Scanner(System.in);

int valid = 0;

while(valid==0){

if(input.equalsIgnoreCase("q")){
System.out.println("Thank you for using this progam");
return;
}
}

System.out.println("Prime");
break;

}

int num= Integer.parseInt(input);
int lgdiv=LargestDivisor(0);
int lset=LargestSet(0);
System.out.println("Composite");
System.out.println("Largest number that divides "+num+ " evenly is "+  lgdiv);
System.out.println("Largest integer factor set = "+lset+" * "+lgdiv);
break;

}

}

}
}

2. Senior Member
Join Date
Jan 2013
Location
Northern Virginia, United States
Posts
6,226
Rep Power
14

Re: Problem with a loop

Still not certain where you want to loop or why? I notice you set valid to 0. If you are not going to set value to some non-zero number to exit the loops then you might as well use while(true).

Regards,
Jim

3. Member
Join Date
May 2013
Posts
12
Rep Power
0

Re: Problem with a loop

What I need to loop is the main method which recalls all the other methods.

It essentially outputs like this:

Please enter a positive integer: 93
COMPOSITE
Largest number that divides 93 evenly is 31
Largest integer factor set = 3 * 31

4. Re: Problem with a loop

My 0.02c

The primeTest method (correct case) should only test if a value is prime number or not. All the user interface stuff should be in the main method. Then the primeTest method returns a boolean back to main to indicate if the value was prime or not.

5. Member
Join Date
May 2013
Location
New Hampshire, USA
Posts
5
Rep Power
0

Re: Problem with a loop

Hey,

First, to help with your question. It sounds like what you're trying to do is run the program until the user enters 'q.'

Almost all of the functionality of your program runs in the PrimeTest() function, and that is only called once since your main() method runs exactly once, and only calls PrimeTest() once. What you'll need to do in order to get the program to run the way you want it to is to put the call to PrimeTest() inside the while loop in your main() method.

That fix is very simple, however, I'd recommend taking a very fine tooth comb through the rest of your code. Semantically, it may work, but there are a number of things that are simply unnecessary or bad style.

• The scanner in the main() method is unnecessary as it's never used.
• Most of your valid variables are never actually used. They remain 0 the entire time, and the loops are all terminated with break and return statements.
• Only class names are typically fully capitalized (i.e. MyClass, TestPanel, etc.), where it's considered good style to keep variable and method names capitalized for every word except the first (i.e. primeTest, someVariable, anotherThing, longerVariableName, etc.)
• While it technically works thanks to variable scope, it's best to avoid using the same variable name in multiple different contexts (answer is used as a local variable in main() and PrimeTest()).
• As Junky said, it's also best to keep each method other than main() very specialized. Each method should solve one problem generally. Then main() just controls the flow and handles user interface.
• Comments are sexy. Enough said.

Again, these will not affect the functionality of your program, but will make it much more readable. I'm not trying to be a jerk, but if you really like to code and plan to do it for a career or even as something you'll simply continue to do it, it's a good idea to develop good style habits. It makes code easier to maintain and read over time (:

Let us know if you need any further help or advice,

Richard
Last edited by RichardPressler; 06-03-2013 at 04:00 PM.

Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•