# Please Help

Marving Guildon
Greenhorn
Posts: 9
The instruction for the homework are as follow:
The instruction for the homework are as follow:
"Write a program using an array that prints out the counting numbers form 1 through 60 that can be divided evenly by 4. The program should print out the numbers from left to right on your DOS screen wiht no more thatn 4 numbers on a line and at least three spaces between numbers. Also print out the number of numbers and the sum of all these numbers that can be evenly divided by 3."
The following is the code that I have so far:
/

public class ArrayTableXXX
{
public static void output()
{
/* Declare the variables used in the program. */
int x, y, num, sum, junk;
/* Initialize the variable num to one. */
num = 0;
/* This statement declares a multi-dimentional
array with 15 elements named anArray. */
int[][] anArray = new int[4][4];
/* Tells the user what the program will perform. */
System.out.println("This program outputs the counting ");
System.out.println("numbers divisible by 4 starting at 60 in ");
System.out.println("descending order using a multi-dimentional array.");
System.out.println("The output will be shown is two variations.");
System.out.println(" ");
/*This for loop assigns values the array named anArray using
four rows with four numbers in each row. */
for(x=0; x<4; x++)
{
for(y=0; y<4; y++)
{
anArray[x][y]=num * 4;
num++;
}
}
/*The following code uses two for loops
to print out the array named anarray using
four rows with four numbers in each row. */
for(x=0; x<4; x++)
{
for(y=0; y<4; y++)
{
anArray[x][y]=(num -1) * 4;
num--;
}
}

/*The following code uses two for loops
to print out the array named anarray using
four columns with four numbers in each row. */
for(x=0; x<4; x++)
{
for(y=0; y<4; y++)
{
/* This if statement is used to add two
extra space in the output of the array
value if the value.*/
if(anArray[x][y] < 100)
{
System.out.print(" " + anArray[x][y]);
System.out.print(" ");
}
else
{
System.out.print(anArray[x][y]);
System.out.print(" ");
/* This if statement prints a blank
line after all the elements in the
array have been printed. */
if (y == anArray.length)
{
System.out.println(" ");
}
}/*end of else -if statement*/
}/*end of for loop*/
System.out.println(" ");
}
System.out.println(" \n\n");
num = 1;
sum = 0;
count = 0;
/* This statement declares a multi-dimentional
array with 15 elements named anArray. */
int[][] twoArray = new int[5][4];
for(x=0; x<5; x++)
{
for(y=0; y<4; y++)
{
twoArray[x][y]=num * 3;
num++;
}
}

/*The following code uses two for loops
to print out the array named anarray using
four columns with four numbers in each row. */
for(x=0; x<5; x++)
{
for(y=0; y<4; y++)
{
/* This if statement is used to add two
extra space in the output of the array
value if the value.*/
if(twoArray[x][y] < 100)
{
System.out.print(" " + twoArray[x][y]);
System.out.print(" ");
}
else
{
System.out.print(twoArray[x][y]);
System.out.print(" ");
/* This if statement prints a blank
line after all the elements in the
array have been printed. */
if (y == twoArray.length)
{
System.out.println(" ");
}

sum = sum + twoArray[x][y];

/*count the numbers*/
count = count 1;

}/*end of else -if statement*/
}/*end of for loop*/
System.out.println(" ");
}
System.out.println(" ");

} /* end of output method */
public static void main(String[] args)
{
ArrayTableXXX.output();
}
}/*end of ArrayTableXXX.*/

Thanks for your help.
[ September 28, 2003: Message edited by: Marving Guildon ]
[ September 28, 2003: Message edited by: Marving Guildon ]

Joel McNary
Bartender
Posts: 1840
Welcome to JavaRanch, Marving.
Just let me say that you will get better responses to your posts if you use the [CODE] and [/CODE] tags in you posts -- it will preserve the formatting of your code and make it easier to read.
That said, the first error is telling you that you have not defined a variable named index anywhere.
The second error tell you that you are trying to add an array to an integer. This doesn't make sense to the compiler, and doesn't really make sense to me...did you mean to say "sum = sum + twoArray[x][y];"?
The next two errors state that you have not defined a variable nmed count anywhere. You should state somewhere "int count = 0;"
If you make these changes, you will get an error stating that "warning, variable sum may not have been initialized." When you declare a method variable, it does not have a default value (not even 0), so you have to assign it a value before you use it. Just say "int sum = 0;" where you declare it and you should be fine.
============================================
That said, I'm a little bit confued about your approach here. It seems to be a lot more complicated than it needs to be, particularily through your use of a multi-dimensional array. In fact, that multi-dimensional array is what is causing your training zero. Arrays, unlike method variable, are initialized to 0, 0.0, false, or null (whichever is appropriate for the type). Since these are int arrays, they get initialized to 0. Is it just personal preference to print the number out in a 4x4 grid, or is it some aspect of the assignment that I missed? The assignment says to print them out from left to right, so printing them in a 4x4 grid might actually get points taken off.
Allow me to make a few suggestions:
First, separate the functions. Have one to do the divisible by four numbers, and another to do the divisible by three numbers. Or, if your slick, you could write a generic function that would handle any upper limit and any "divisible by" number.
Second, use a java.util.List to hold the results instead of an Array. Since you don't know the number of slots you need beforehand (well, you do in this case, but if your finction is generic you don't...), then teh dynamic sizing of the List will be useful. Remember that you can't put ints into a List, so you will have to wrap them using java.lang.Integer classes. (BTW, Java version 1.5 will introduce "autoboxing" that will do this for you, but for now you have to do it manually...)
Finally (and this is jsut style), use "//" instead of "/* ... */" for commentary on the code, unless you're writing javadoc (where you have to use "/** ... */"). Reserve "/* ... */" for commenting out large sections of code. This is because "/* ... */" comments cannot be nested.

Saraswathi
Greenhorn
Posts: 7
hi ,
I am new to java programming , and may not be able to explain the very detail of your programs mistake but ofcourse give u the solution.

first: [index] ---------------

You need to give the the variables that describes the dimensions i.e [x][y],
like this
sum = sum + twoArray[x][y];
second: sum and count variables are not initialized to 0 .

third : + operator will be cleared if you change the word index to [x][y].
Hope it helps
Thank you.
Good Luck.