aspose file tools*
The moose likes Beginning Java and the fly likes Initializing a one dim array ??? need help plz Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Initializing a one dim array ??? need help plz" Watch "Initializing a one dim array ??? need help plz" New topic
Author

Initializing a one dim array ??? need help plz

Douglas Braxton
Ranch Hand

Joined: Jan 28, 2004
Posts: 36
I am trying to input an integer thru JOptionPane and have that number become the size of a one dimensional table. The MyDriver main calls the method class MyArray.
i am getting a compile error on my System.out.println because "counter" is unresolved.
Any idea why or what i am doing wrong and whether the scheme will work otherwise?
???
thanx
djb
//START main
import javax.swing.JOptionPane;
import java.text.*;
public class MyDriver
{ public static void main (String[ ] arg)
{ String firstNumber;
String kVal, fVal, minVal, maxVal, medVal;
int x;
firstNumber = JOptionPane.showInputDialog("Enter an integer number:");
try
{ x = Integer.parseInt( firstNumber ); }
catch ( NumberFormatException nfe)
{ x = 0; }
MyArray my = new MyArray( x );
my.fill();
}//END main
//START method class
public class MyArray
{ private int [ ] a; // int array declaration as private
MyArray ( int x ) { a = new int [ x ]; }
void fill()
{ for (int counter = 0; counter < a.length; counter++)
a[ counter ] = 100 + ( int ) ( Math.random () * 100 );
System.out.println(counter +" " +a[ counter ]); }
}//END method class
[ February 26, 2004: Message edited by: Douglas Braxton ]
[ February 26, 2004: Message edited by: Douglas Braxton ]
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 10916
    
  12

This one's not too bad (at least, i can fix the compilation error).
in your for statment, you delcare your int counter. This makes it local to the for loop. once you exit the loop, counter is out of scope.
since you don't have brackets after the for statement, ONLY THE NEXT LINE is in the loop. in other words, you effectively have this:

so counter is out of scope on the println line.
you could fix this by doing this:

or, you could put brackets around the two lines after the for, which would then print every value.
[ February 26, 2004: Message edited by: fred rosenberger ]

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Douglas Braxton
Ranch Hand

Joined: Jan 28, 2004
Posts: 36
thanks a lot for that help.
i braced it recompiled and it printed out my array elements pretty as you please.
0 199
1 121
2 133
3 179
4 114
5 147
6 170
7 115
8 122
9 188
10 160
11 191
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 10916
    
  12

That's one of the reasons why i ALWAYS use braces for EVERY for loop. it's easy to overlook a line is NOT part of the block, especially when it's indented.
Glad it worked out for you.
Keep on Codin'
sever oon
Ranch Hand

Joined: Feb 08, 2004
Posts: 268
Read some good tips on how not to write code. You'll love it, trust me.
I've actually run across some bad code over the years, and I've adopted some strict style rules to avoid inflicting things of this nature on other people. Here's one of my all-time favorites (changed slightly for the purpose of this example):

How many people do you think looked at the code before they detected the problem? (The println() statement isn't part of the for loop because of the misplaced ")".)
Here's another that literally dozens of people looked at and couldn't find the bug:

For some odd reason, bar would *always* be 0, no matter what we set it to. Finally, after scrutinizing this code for several minutes straight, I saw this issue:

Now, as a rule, I never, ever code like this. I always let the method name do the explaining and I use an abbreviated form in the parameter list:

So there's never a name conflict between a class-level variable and a local variable.
Here's another good one from the annals:

Did you notice in this code that arr[i][3] will not get set? (It's commented out.)
There's all sorts of these little bugaboos. Wait until the first time you run up against nested switch statements, or a nested ternary. Or how about:

Does anyone among us have any idea what value k will have?
Happy hacking,
sev
[ February 28, 2004: Message edited by: sever oon ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Initializing a one dim array ??? need help plz
 
Similar Threads
Enter # in JTextField, draw = # ovals, use JBottons to color ovals???
BufferedImage ?
Array problem
One DIM array ??? continued
How to get a method to call a method