public class csedersten_Perfect { /* * * * */ public static void main ( String args[] ) { int getN; int getP = getPerfect(); int [] perfect = new int[100]; int x = 1; boolean test = testPerfect(getP, perfect); while (x <= perfect.length) { printFactors(getP, perfect, test); } } /* * * * */ public static int getNum() { Scanner i = new Scanner( System.in ); int num; // int perfectNum = 1; // int total = 0; // System.out.print( "How many numbers would you like to test? "); num = i.nextInt(); boolean validateN = validateNum(num, perfectNum); while (!validateN) { System.out.print( "How many numbers would you like to test? "); num = i.nextInt(); validateN = validateNum(num, perfectNum); } return num; } /* * * * */ public static boolean validateNum( int num, int perfectNum ) { if (( num <= 0 ) || ( perfectNum <= 0 )) { System.out.println( " Non-positive numbers are not allowed." ); } else { return true; } return false; } /* * * * */ public static int getPerfect() { Scanner i = new Scanner ( System.in ); int perfectNum = -1; int num = getNum(); System.out.print( "Please enter a possible perfect number: " ); perfectNum = i.nextInt(); boolean validateN = validateNum(perfectNum, num); while (!validateN) { System.out.print( "Please enter a possible perfect number: " ); perfectNum = i.nextInt(); validateN = validateNum(perfectNum, num); } return perfectNum; } /* * * * */ public static boolean testPerfect( int perfectNum, int[] perfect ) { int max = perfectNum; int a = 0; int sum = 0; for (int x = 1; x <= max; x++) { perfect[a++] = x; perfectNum /= x; } perfect[a] = perfectNum; for (int x = 0; x < perfect.length; x++) { sum += perfect[x]; } if (sum == perfectNum) { int[] p = perfect; return true; } else { return false; } } /* * * * */ public static void printFactors( int perfectNum, int[] perfect, boolean test) { if (test == true) { System.out.printf( "%d:%10d", perfectNum, perfect); } else { System.out.printf( "%d:NOT PERFECT", perfectNum ); } } }

## 5 Replies - 1018 Views - Last Post: 14 April 2013 - 07:40 PM

### #1

# Determine perfect number, error with infinite loop

Posted 14 April 2013 - 06:41 PM

I am having two issues with my program. 1) I cannot figure out how to loop to the next determination when the user enters more than one number. 2) How to print if a number is perfect or not perfect without it going into an infinite loop or printing it the same number of times as there are spots in the array.

##
**Replies To:** Determine perfect number, error with infinite loop

### #2

## Re: Determine perfect number, error with infinite loop

Posted 14 April 2013 - 06:49 PM

Your while loop is riding on the integer x being less than 100, it is set to 1. There is nothing in your code that will make x ever be more than 100, hence, you have an infinite loop.

### #3

## Re: Determine perfect number, error with infinite loop

Posted 14 April 2013 - 07:06 PM

I understand what you're saying, but when I enter a number to determine if it is perfect or not perfect, the program doesn't stop printing the same thing over and over. I have to type ctrl c to escape it. I was wondering how to fix that.

### #4

## Re: Determine perfect number, error with infinite loop

Posted 14 April 2013 - 07:19 PM

You obviously didn't understand what I said, because addressing the issue I pointed out would correct your infinite loop that's printing everything out.

The int 'x' is 1.

The length of your array 'perfect' is [100]

You never change x in your code. x is always <= perfect.length.

I think you are trying to track how many inputs there are, an array length is not the way to do this since arrays have a set length at birth. What you will want to do is have a variable to record the number of inputs, and use that value in a for loop.

while (x <= perfect.length)

The int 'x' is 1.

The length of your array 'perfect' is [100]

You never change x in your code. x is always <= perfect.length.

I think you are trying to track how many inputs there are, an array length is not the way to do this since arrays have a set length at birth. What you will want to do is have a variable to record the number of inputs, and use that value in a for loop.

//class variable static int numberOfInputs = 0; //in your getPerfect() method System.out.print( "Please enter a possible perfect number: " ); perfectNum = i.nextInt(); numberOfInputs++; //your for loop for(int i = 0; i < numberOfInputs; i++) { print }

### #5

## Re: Determine perfect number, error with infinite loop

Posted 14 April 2013 - 07:28 PM

This does not really make sense

so you store

perfect[0] = 1

perfect[1] = 2

perfect[2] = 3

.... up to

perfect[max]

at the same time assuming perfectNum == 10 which mean max == 10

you will

perfectNum /= 1

perfectNum /= 2

...

perfectNum /= 10

sure that perfectNum will be == 0 at the end of the loop

int max = perfectNum; int a = 0; int sum = 0; for (int x = 1; x <= max; x++) { perfect[a++] = x; perfectNum /= x; }

so you store

perfect[0] = 1

perfect[1] = 2

perfect[2] = 3

.... up to

perfect[max]

at the same time assuming perfectNum == 10 which mean max == 10

you will

perfectNum /= 1

perfectNum /= 2

...

perfectNum /= 10

sure that perfectNum will be == 0 at the end of the loop

### #6

## Re: Determine perfect number, error with infinite loop

Posted 14 April 2013 - 07:40 PM

Flukeshot, I would use that solution if I could, but I am not allowed to use global variables for this assignment.

Page 1 of 1