Results 1 to 5 of 5
  1. #1
    grrttrdsll is offline Member
    Join Date
    May 2013
    Posts
    12
    Rep Power
    0

    Default 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")) {
    							return answer=2;
    							
    						}
    						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) {
    					
    					return answer=1;
    					}
    					
    					if(num%i==0){ 
    						
    						return answer=0;
    					}
    					else if(i==2){
    						
    						return answer=1;
    					}
    				}
    					
    			}
    			
    		}	
    
    		
    			
    		
    		
    		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 answer=PrimeTest(0);
    			
    			
    			
    			
    			int valid = 0;
    		
    			
    			
    			while(valid==0){
    				
    				if(answer==2){
    					if(input.equalsIgnoreCase("q")){
    					System.out.println("Thank you for using this progam");
    					return;
    					}
    				}	
    				
    				if(answer==1){
    					
    					System.out.println("Prime");
    					break;
    					
    				}
    				
    				if(answer==0){	
    					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. #2
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,763
    Rep Power
    5

    Default 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
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    grrttrdsll is offline Member
    Join Date
    May 2013
    Posts
    12
    Rep Power
    0

    Default 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. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default 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. #5
    Join Date
    May 2013
    Location
    New Hampshire, USA
    Posts
    5
    Rep Power
    0

    Default 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.

Similar Threads

  1. problem with for loop
    By cherrychives in forum New To Java
    Replies: 7
    Last Post: 04-23-2012, 03:18 PM
  2. Replies: 2
    Last Post: 11-07-2011, 02:25 AM
  3. problem in do while loop
    By Dayanand in forum New To Java
    Replies: 2
    Last Post: 02-09-2011, 12:04 PM
  4. simple line problem / for loop problem
    By helpisontheway in forum New To Java
    Replies: 1
    Last Post: 11-17-2009, 06:12 AM
  5. While-loop problem
    By jimmy-lin in forum New To Java
    Replies: 6
    Last Post: 11-02-2009, 03:10 AM

Posting Permissions

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