# Calculating Squares and Cubes in a table

• 03-22-2010, 07:07 AM
aldorfski_17
Calculating Squares and Cubes in a table
Hey All,

I'm sorry if I sound rushed, but I need some help with one of my assignments. I am having some issues coding my "for" loops. I have an assignment that requests a table that receives an integer input. Once I input an integer, the table will display all of the squared and cubed numbers of every number leading up to that input. See example below:

enter integer: 3

Number Square Cube
1 1 1
2 4 8
3 9 27

continue? (y/n):

The code I have just keeps repeating and does not increment. I tried incrementing up to the input, but that didn't work either. Any help is greatly appreciated. Thanks.

-Aldorfski

See code below:

Code:

```import java.util.Scanner; public class PowerTableDisplayer {         public static void main(String[] args) {                 // TODO Auto-generated method stub                                 // Code to display "Welcome to the Squares and Cubes Table                 System.out.println("Welcome to the Squares and Cubes Table");                 System.out.println();                                 // Create a Scanner object                 Scanner sc = new Scanner(System.in);                                                                 String choice = "y";                 while(choice.equalsIgnoreCase("y"))                 {                             // get the input from the user                                 System.out.println("Enter an Integer: ");                 int integerNext = sc.nextInt();                                                  System.out.println("Number" + "  " + "Squared" + "  " + "Cubed");                                      System.out.println("======" + "  " + "======" + "  " + "======");                                       for (int i = 1; i > 0; i++)         {              i = integerNext;         int numberSquared = (int) Math.pow(i, 2);         int numberCubed = (int) Math.pow (i, 3);                         String message = "\n" + i + "      " + numberSquared + "      " + numberCubed;                 System.out.println(message);         System.out.println();                 // see if the user wants to continue         System.out.print("Continue? (y/n): ");         choice = sc.next();         System.out.println();                 }                                 }                                                 }                                                           }```
• 03-22-2010, 07:17 AM
m00nchile
Well, when you start your for loop, you set the value of i to integerNext, so every iteration works with the same value. I think a much better way would be like this:
Code:

```for(int i = 1; i <= integerNext; i++) { //the rest of the code goes here }```
And learn to use code tags and proper indentation, readability is important, especially if you want others to read your code.
• 03-22-2010, 07:40 AM
aldorfski_17
Getting there
Hey MoonChile,

Thanks for your reply. After implementing the change, the program ends after the first line.

Enter integer: 3

Number Squared Cubed
3 9 27

Any thoughts on how I can get the Number 1 and 2 to show up, as well as the Square and Cubed numbers for each?

-Aldorfski
• 03-22-2010, 08:43 AM
JosAH
Remove this line from the body of your loop:

Code:

`i = integerNext;`
kind regards,

Jos
• 03-22-2010, 08:17 PM
aldorfski_17
Thanks
Hey All,

Thanks for your help. This is great.

-Aldorfski