Problem with a Variable
I was just trying to make a simple program to test out using a variable with multiple values, being a new Java student I just try out different things to try and see how they work. For some reason this wouldn't compile right, any tips?
//--
// Trying out using one variable with multiple values.
// 9/03/2006
//--
publicclass MultiVarTest
{
publicstaticvoid main(String[] args)
{
int a[] ={3,6,10,-8}, n;
for (n = 0; n < 3; n++);
{
System.out.println("2 x "+ a[n] +" = "+ (2 * a[n]));
}
}
}
[1211 byte] By [
tuptaina] at [2007-10-3 4:10:26]

What are the errors the compiler states and what is the stack trace?That will help us help you.JJ
It only loops through once printing out the last value in the variable. So to say it doesn't compile isn't right, it doesn't do what I want it to, which is to print out that text but with each value in the variable. Sorry. To me, that should come out as:
2 x 3 = 6
2 x 6 = 12
2 x 10 = 20
2 x -8 = -16
but instead it only displays
2 x -8 = -16
It's probably an obvious answer but I don't see the problem.
Use this instead:for(n = 0; n < a.length; n++)And is that your exact code?
It is, change that to a.length gave me the error
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 4
at MultiVarTest.main(MultiVarTest.java:14)
My edited code is now:
//--
// Trying out using one variable with multiple values.
// 9/03/2006
//--
public class MultiVarTest
{
public static void main(String[] args)
{
int a[] = {3,6,10,-8}, n;
for (n = 0; n < a.length; n++);
{
System.out.println("2 x "+ a[n] + " = "+ (2 * a[n]));
}
}
}
so your looking for this output:2 x 3 = 62 x 6 = 122 x 10 = 202 x -8 = -16JJ
Delete the semicolon at the end of your for loop.
Lol. Told you it was simple. /sigh, thanks.
No problem. I almost missed it :)
I slightly modified your method.
//--
// Trying out using one variable with multiple values.
// 9/03/2006
//--
public class MultiVarTest
{
public static void main(String[] args)
{
int a[] = {3,6,10,-8};
for (int n = 0; n < a.length; n++)
{
System.out.println("2 x " + a[n] + " = " + (2 * a[n]));
}
}
}
It's not good practice to declare multiple variables on one line. Also initialize all variables when you declare them, when ever possible.
I like to declare counter variables within the for loop as I have shown. It helpes with variable scopeing and lifetime.
That way your n counter will only exist within your for loop. The way you coded it, n exists after the for loop ends, which could be bad.
JJ
Message was edited by:
Java_Jay
One reason I prefer braces at the end of the line, makes spotting these mistakes easier because you have to look at the end of the line to find the brace.
for (int n = 0; n < a.length; n++); {
System.out.println("2 x " + a[n] + " = " + (2 * a[n]));
}
> One reason I prefer braces at the end of the line,
> makes spotting these mistakes easier because you have
> to look at the end of the line to find the brace.
> > for (int n = 0; n < a.length; n++); {
> System.out.println("2 x " + a[n] + " = " + (2 *
> a[n]));
>
>
like that ; between the ) and { ?
I agree totally.
JJ