Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Array Declaration- please help 2

Status
Not open for further replies.

MissouriTiger

Programmer
Oct 10, 2000
185
US
I can't believe I'm getting 9 errors from a single array declaration. I sure would be grateful if someone would tell me (show mw) what I'm doing wrong here.

final double dblPlanChgs[][] = new double[3][3];
dblPlanChgs[0][] = {10.00,35,0};
dblPlanChgs[1][] = {0.00,25,10};
dblPlanChgs[2][] = {5.00,15,5};

Also, would anylike to recommend an IDE? I'm using JEDPlus, which I like, but it's extremely basic. It doesn't do a lot of debugging for you.
 
You need to do this:

final double dblPlanChgs[][] = {{10.00,35,0}, {0.00,25,10}, {5.00,15,5}};

 
The following works too:

final double dblPlanChgs[][] = new double[3][];
dblPlanChgs[0] = new double[] {10.00,35,0};
dblPlanChgs[1] = new double[] {0.00,25,10};
dblPlanChgs[2] = new double[] {5.00,15,5};

As an IDE I'd strongly recommend Borland's JBuilder. But this is just a personal preference. Other people I know use Kawa, but they mostly also changed to JBuilder. Best of all: there's a free version - go to
Hope this helps
allow thyself to be the spark that lights the fire
haslo@haslo.ch - www.haslo.ch​
 
Thanks meadandale, that works great! I don't understand why you don't have to use the "new" keyword. I have a book that specifically says to use the "new" keyword, then the author turns around and without any explanation he doesn't use it. What gives?

Haslo, I tried it your way and it won't compile. I even tried copy/pasting your code directly. It generates the 9 errors I was complaining about. See man, I gotta find a better environment. It takes me 3 days just to get a few lines of code to run. I've never had this kind of trouble with a language.

I tried the free version of JBuilder a couple of months ago. I installed it, but every time I started it up, it would lock up my system. I have a reasonably powerful computer, and have never had any problem running big software prorams, but I had to uninstall JBuilder- it just wouldn't run. Any suggestions?

 
Do you have the code in the constructor of the class or above it? It really doesn't work if you try to reference an object that is not really instantiated - and before the constructor of your class pretty much nothing is being instantiated at all...

With your JBuilder question: I don't know what your problem may be - other big Java apps run, too?
Well, there may be a solution ahead: Inprise / Borland just announced that they'll bring out the next Version of JBuilder this or next month - why not just wait so long? ;-)
allow thyself to be the spark that lights the fire
haslo@haslo.ch - www.haslo.ch​
 
Constructor of what class? It's in the variable declarations of my Applet. See below:

public class SaveLongMoney extends Applet implements ActionListener
{
final double dblPlanChgs[][] = new double[3][];
dblPlanChgs[0] = new double[] {10.00,35,0};
dblPlanChgs[1] = new double[] {0.00,25,10};
dblPlanChgs[2] = new double[] {5.00,15,5};
...

My compiler just spits it out. It says things like "Identifier expected" and "cannot resolve symbol"

But fortunatley meadandale's way works.

Yeah, I'll try the next version of JBuilder when it comes out.

 
Yes, right, just that class - the one that extends Applet :). Try the following:

public class SaveLongMoney extends Applet implements ActionListener
{
final double dblPlanChgs[][];
public SaveLongMoney()
{
dblPlanChgs = new double[3][];
dblPlanChgs[0] = new double[] {10.00,35,0};
dblPlanChgs[1] = new double[] {0.00,25,10};
dblPlanChgs[2] = new double[] {5.00,15,5};
}
...

That should do the trick.

btw: I prefer inner classes that implement ActionListener to the main class being an action listener itself - mostly I use just the following anonymous syntax if I have to add one of those ActionListeners:

object.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// do action
}
});

This leads to way less confusion if you have more than, say, two objects that need an action listener.
allow thyself to be the spark that lights the fire
haslo@haslo.ch - www.haslo.ch​
 
You don't have to use the word new for the same reason you don't have to use the word new when you do:

String myString = "Isn't this nifty?";

The compiler puts it in for you.

The reason that your way didn't work is that you declare and allocate the array in the first line but then try to use the initializer syntax after the array is already created. What would SEEM to work is the following:

final double dblPlanChgs[][] = new double[3][];
dblPlanChgs[0] = {10.00,35,0};
dblPlanChgs[1] = {0.00,25,10};
dblPlanChgs[2] = {5.00,15,5};

Here you create the array of array references but the array references are initially null (recall that java multi-d arrays are arrays of arrays). Then you initialize the actual arrays. It turns out that this DOESN'T work though so you have to use the syntax that Haslo provided.

In your case, however, since you know what the arrays are going to hold initially anyways, it makes more sense to just create the array using the initializer syntax I showed you.


In regards to JBuilder, I use JBuilder 4 Professional and Enterprise and I love it. It is quite the memory hog though so make sure you have plenty of RAM. I'd suggest 256 MB as a mimimum.

Regards

Charles
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top