Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: help im stumped :(

  1. #1
    Junior Member
    Join Date
    Nov 2010
    Posts
    28
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default help im stumped :(

    int num;
    int i = 2;
    int j;
    String yn;




    do {
    System.out.print("Enter number : ");
    num = input.nextInt();

    do{
    while (num % i == 0){
    j = num/i;
    num = j;
    System.out.print(i + " ");

    if (num % i != 0)
    i++;
    }



    }
    while (num % i == 0);


    System.out.println();
    System.out.println("\nDo you Wish to enter another? (Y/N) : ");
    yn = input.next();

    }

    while (yn.charAt(0) == 'Y');





    }
    }


    if i enter 120 it only outputs 2 2 2 3,



    but it needs to output 2 2 2 3 5


    what am i doing wrong?


    and also how can i println

    The Factors are : 2 2 2 3.... a continous output...


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,310
    Thanks
    56
    Thanked 2,373 Times in 2,344 Posts

    Default Re: help im stumped :(

    Have you worked thru the algorithm you are using manually?
    Does it produce the answers you want?

    Do you want to debug your code or are you asking some one else to debug it for you?
    If you want to debug it, Add some debugging print outs to show ALL the values of the variables and the computations as the loop progresses.

  3. #3
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: help im stumped :(

    Also, I think your post may have been answered already in your other thread.

  4. #4
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: help im stumped :(

    import java.util.Scanner;
     
    public class Factor {
     
    	public static void main(String[] args){
    		Scanner input=new Scanner (System.in);
     
    		String yn;
    		int number;
    		int current;
    		String output = "";
    		int x;
    		int i = 2;
     
    		System.out.print("Enter a number to factor : ");
    		number = input.nextInt();
     
     
     
     
    		while (number % 2 == 0)
    		{
    			output += 2 +" ";
    			number /= 2;
    		}
     
    		x = number;
     
    		while (i <= x)
    		{
    			if (x % i == 0)
    			{
    				output += i + " ";
    				x /= i;
    				//I added from here
    				if(x % i == 0){
    					i--;
    				}
    				//to here
    			}
    			i++;
    		}
     
     
     
    		System.out.println("Factors are : " + output);
     
    	}
    }

  5. #5
    Member DavidFongs's Avatar
    Join Date
    Oct 2010
    Location
    Minneapolis, MN
    Posts
    107
    Thanks
    1
    Thanked 45 Times in 41 Posts

    Default Re: help im stumped :(

    import java.util.Scanner;
     
    public class Factor {
     
    	public static void main(String[] args){
    		Scanner input=new Scanner (System.in);
     
    		int number;
    		String output = "";
    		int x;
    		int i = 2;
     
    		System.out.print("Enter a number to factor : ");
    		number = input.nextInt();
     
     
     
     
    		while (number % 2 == 0)
    		{
    			output += 2 +" ";
    			number /= 2;
    		}
     
    		x = number;
     
    		while (i <= x)
    		{
    			while(x % i == 0)
    			{
    				output += i + " ";
    				x /= i;
    			}
    			i++;
    		}
     
     
     
    		System.out.println("Factors are : " + output);
     
    	}
    }

    I know I posted the solution javapenguin pasted here, but this slightly different solution makes it more obvious what is going on.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,310
    Thanks
    56
    Thanked 2,373 Times in 2,344 Posts

    Default Re: help im stumped :(

    makes it more obvious what is going on.
    Why don't you comment your code to actually tell anyone what is going on?

  7. #7
    Member DavidFongs's Avatar
    Join Date
    Oct 2010
    Location
    Minneapolis, MN
    Posts
    107
    Thanks
    1
    Thanked 45 Times in 41 Posts

    Default Re: help im stumped :(

    Quote Originally Posted by Norm View Post
    Why don't you comment your code to actually tell anyone what is going on?
    Because it isn't my code..... It is the OP's from his last thread on this same exact topic. Why don't you comment other peoples homework code for them? Go ahead, the code is right there. Might as well turn it in for him too.

    And outside of the code snippet in that thread, I did explain what the code I added was doing. Thanks for your insight though, it is really helpful!

Similar Threads

  1. Help so stumped....
    By Macgrubber in forum Loops & Control Statements
    Replies: 8
    Last Post: October 28th, 2010, 04:53 PM
  2. Stumped?
    By KevinGreen in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 3rd, 2009, 02:02 AM