Morgan
Morgan

Reputation: 133

Simple java troubles

I know it's not a big deal, but I hate what my program is doing. Here is the simple program I haWrite a program that asks the user for an integer and then prints out all of its factors. For example, when the user enters 150, the program should print 2, 3, 5, 5.

Here is my program (it works)

import java.util.Scanner; 

public class FirstProgram {  
    public static void main(String[] args) {  

        Scanner scan = new Scanner(System.in);  
        int number;
        int divisor = 2;  

        System.out.println("Enter the number: ");  
        number = scan.nextInt();  
        while (number > 1) {  
            if ((number % (number / divisor)) == 0 && number > 0) {  
                number = number / divisor;  
                System.out.print(divisor);  
                if (number != 1)  
                    System.out.print(", ");  
            } 
            else if ((number % (number / divisor)) != 0 && number > 1) { 
                divisor++;  
                if (number == 1) {
                    System.out.println(number);
                    System.out.println();
                }
            }   
        }  
    }  
}

the stupid problem that is driving me nuts is the final line in the transcript. When it prints 2, 3, 5, 5 the program ends and its ends on the same line. Here is a picture because I can't describe things:

enter image description here

I tried a few things, but being new at programming I didn't have many options to try. Thanks for the help!

Upvotes: 1

Views: 95

Answers (4)

Mark Butler
Mark Butler

Reputation: 496

Your problem is that the statement

System.out.println();

Isn't executed because when you have one number left

while (number > 1) == false

If you move the statement outside the while loop at the bottom of the method it will work properly.

Also have a think about how you can refactor the code as I cannot see the block

if (number == 1) {
  System.out.println(number);
  System.out.println();
}

Would ever be executed due to the problem I've described above. (while number > 1)

Mark

Upvotes: 0

CmdrMoozy
CmdrMoozy

Reputation: 3941

With some nicer indentation, your code looks like this:

Scanner scan = new Scanner(System.in);  
int number;
int divisor = 2;  

System.out.println("Enter the number: ");  
number = scan.nextInt();  
while (number > 1)
{
    if ((number % (number / divisor)) == 0 && number > 0)
    {  
        number = number / divisor;  
        System.out.print(divisor);  
        if (number != 1)  
        System.out.print(", ");  
    }
    else if ((number % (number / divisor)) != 0 && number > 1)
    { 
        divisor++;  

        if (number == 1)
        {
            System.out.println(number);
            System.out.println();
        }
    }   
}

The problem you're having is that, for some numbers, the else if ... portion of your loop is never reached. Restructure the body of the loop like this, and I think it will do what you want:

while (number > 1)
{
    if ((number % (number / divisor)) == 0 && number > 0)
    {  
        number = number / divisor;  
        System.out.print(divisor);  
        if (number != 1)  
        System.out.print(", ");  
    }
    else if ((number % (number / divisor)) != 0 && number > 1)
    { 
        divisor++;  

        if (number == 1)
        {
            System.out.println(number);
        }
    }   
}

System.out.println();

Note that the final println() happens after the loop prints out all of the factors, but it happens regardless of what the input number was.

Upvotes: 0

Vidya
Vidya

Reputation: 30310

Whenever you need to move to the next line, just use System.out.println(); You just need that at the end of your program.

Upvotes: 0

Java Devil
Java Devil

Reputation: 10969

At the end of your main method just add

System.out.println();

So that it ends on a new line

Upvotes: 6

Related Questions