# Printing multiple arrays help...

• 01-24-2013, 06:31 AM
Toffer_15
Printing multiple arrays help...
Let me say first off that I am new to the forum and new to Java. I'm not looking for someone to write code for me, just some understanding. I'm in a basic Java class, and I need to make a table with the base range from 0 to 10, a squared range of the base, and a cubed range of the base. It should output like this:

Base Square Cubed
0 0 0
1 1 1
2 4 8
3 9 27
4 16 64
etc. etc.

This is what I have so far, I know it probably has beginner written all over it, but that is what I am.

My problem is this: I can't seem to find a way to output my arrays, I've searched the forums and internet. I saw most answers saying I need to do a for loop. However, I haven't learned this and failed miserably when I tried. If this is the only way to print out multiple arrays, I'll just have to wait and learn, but I was hoping there was an easier way that I haven't found yet. Any help is appreciated. Thanks much:)-:
Oh and I also tried Math.pow but couldn't figure out how to use it within arrays.

Code:

```import java.util.Arrays; import java.util.Scanner; public class HW2e {   public static void main( String[] args )   {       Scanner input = new Scanner( System.in );       int x;                 int y;                 int z;                 int[] A1;                 int[] A2;                 int[] A3;                                 //User Input       System.out.print( "Select the number you'd like the table to begin with: " );       x = input.nextInt();                                                                 //Base Range                 y = x + 11;                 A1 = new int[y];                 A1[x] = x;                 A1[x+1] = x + 1;                 A1[x+2] = x + 2;                 A1[x+3] = x + 3;                 A1[x+4] = x + 4;                 A1[x+5] = x + 5;                 A1[x+6] = x + 6;                 A1[x+7] = x + 7;                 A1[x+8] = x + 8;                 A1[x+9] = x + 9;                 A1[x+10]= x + 10;                                 //Squared Range                 A2 = new int[y];                 A2[x] = x*x;                 A2[x+1] = (x+1)*(x+1);                 A2[x+2] = (x+2)*(x+2);                 A2[x+3] = (x+3)*(x+3);                 A2[x+4] = (x+4)*(x+4);                 A2[x+5] = (x+5)*(x+5);                 A2[x+6] = (x+6)*(x+6);                 A2[x+7] = (x+7)*(x+7);                 A2[x+8] = (x+8)*(x+8);                 A2[x+9] = (x+9)*(x+9);                 A2[x+10]= (x+10)*(x+10);                 //Cubed Range                 A3 = new int[y];                 A3[x] = x*x*x;                 A3[x+1] = (x+1)*(x+1)*(x+1);                 A3[x+2] = (x+2)*(x+2)*(x+2);                 A3[x+3] = (x+3)*(x+3)*(x+3);                 A3[x+4] = (x+4)*(x+4)*(x+4);                 A3[x+5] = (x+5)*(x+5)*(x+5);                 A3[x+6] = (x+6)*(x+6)*(x+6);                 A3[x+7] = (x+7)*(x+7)*(x+7);                 A3[x+8] = (x+8)*(x+8)*(x+8);                 A3[x+9] = (x+9)*(x+9)*(x+9);                 A3[x+10]= (x+10)*(x+10)*(x+10);                               System.out.println(A1, A2, A3);                 //I know this is doesn't work                 } }```
• 01-24-2013, 07:57 AM
pbrockway2
Re: Printing multiple arrays help...
Do you have to use arrays? I'm asking because although you can construct arrays, work out all of the output, then print it, it might be more straight forward to work one line at a time.
• 01-24-2013, 08:10 AM
Toffer_15
Re: Printing multiple arrays help...
Nope, the assignment is actually supposed to be working out all the calculations with a calculator then just printing out the answers in a table, but my professor will give extra credit if I use a user input. This was the easiest user input I could think of. So you're saying doing like a math.pow for each line? Then printing out the results?
• 01-24-2013, 08:43 AM
JosAH
Re: Printing multiple arrays help...
Think of it this way: you have a line 'i' (being at least zero); on that line you want to print the values i, i*i and i*i*i. You want to print N lines, so the smallest value for i is zero and the largest value is N-1; this begs for a simple loop:

Code:

```for (int i= 0; i < N; i++)   // print the content of line 'i'```
The value of N can be defined by use input, by a calculation or whatever; it doesn't change this loop and you don't need arrays either.

kind regards,

Jos
• 01-24-2013, 09:04 AM
pbrockway2
Re: Printing multiple arrays help...
Sorry - I didn't see your response.

First, as Jos says, each line just consists of printing three numbers i, i*i and i*i*i where i is some number. Now you could ask the user where they want to start and then give them 10 lines with ten println() statements. In fact if you are unused to for loops that might be a good place to start. (post back if you try that - or anything - and get stuck. Suggestions given here are just steps along the path of head banging, more head banging, and eventual success.)

Eventually all ten println()s should be wrapped up into a single for loop... Because that's what for loops are for.
• 01-24-2013, 07:41 PM
Toffer_15
Re: Printing multiple arrays help...
Thank you Jos and pbrockway! I'm going to start working on the for loop, but if I still can't figure it out then I'll just println ten separate lines. Thanks again, and I'll post any success or failure on here.
• 01-24-2013, 08:13 PM
Toffer_15
Re: Printing multiple arrays help...
Alright new question. Now I have my loop setup and it works! but I was wondering where the i, i*i, i*i*i, calculations come into play? Would I need to write a nested loop?
Also, is there a way to print the numbers vertically rather than horizontally?:)-:

Code:

```import java.util.Arrays; import java.util.Scanner; public class HW2f {   public static void main( String[] args )   {       Scanner input = new Scanner( System.in );       int x;                 int y;                 int z;                                 //User Input       System.out.print( "Select the number you'd like the table to begin with: " );       x = input.nextInt();                       System.out.print( "Select the number you'd like the table to end with: " );       y = input.nextInt();                                 z = (y - x)+1;                                                 for(int i = x; i<z; i++){                         System.out.print(i);                   }                 } }```
• 01-24-2013, 08:28 PM
JosAH
Re: Printing multiple arrays help...
Simply change your print( ... ) statement to System.out.println(i+" "+i*i+" "+i*i*i); that's all ...

kind regards,

Jos
• 01-24-2013, 09:35 PM
Toffer_15
Re: Printing multiple arrays help...
Okay I see now. I can use the print command to do calculations for me. Thank you Jos and pbrockway for the help. I'm going to leave the finished code for people like me to see. Thanks again. :(y):

Code:

```import java.util.Scanner; public class HW2f {   public static void main( String[] args )   {       Scanner input = new Scanner( System.in );       int x;                 int y;                                 //User Input       System.out.print( "Select the number you'd like the table to begin with: " );       x = input.nextInt();                       System.out.print( "Select the number you'd like the table to end with: " );       y = input.nextInt();                                 //Title Output                 System.out.println  ("Base\tSquared\tCubed");                                 //For Loop with calculated output                 for(int i = x; i<=y; i++){                         System.out.println(i+"\t"+i*i+"\t"+i*i*i);                   }                 } }```