• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Initializing a one dim array ??? need help plz

 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Douglas Braxton
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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'
 
Ranch Hand
Posts: 268
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Won't you be my neighbor? - Fred Rogers. tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic