Initializing array with values from constructor parameter - Impossible to initialize?
Declare trshIt
as a member of the class, but initialize it in the constructor, like this:
boolean [] trshIt;
double [] CO2TrashEmissions = {184.0, 25.6, 46.6, 165.8};
//constructor
CO2FromWaste(int crust, boolean p, boolean pl, boolean gl, boolean ca)
{
myPlot = 1018.0;
myCrust = crust;
myP = p;
myPl = pl;
myGl = gl;
myCa = ca;
trshIt = new boolean[4];
trshIt[0] = myP;
trshIt[1] = myPla;
trshIt[2] = myGl;
trshIt[3] = myCa;
}
Admin
Updated on June 04, 2022Comments
-
Admin almost 2 years
I asked this same question on javaprogrammingforums.com but it seems their website is down right now. So I couldn't see what responses (if any) I got. Anyways, I am badly stuck on this Java HW assignment. What I have so far looks good in terms of completion, now it's just getting the right values to appear. Pretend I have this:
(This is only part of the 2nd class of the two with the constructor, the other is the "tester")
//private variables boolean myP; boolean myPla; boolean myGl; boolean myCa; double myPlot; int myCrust; double myReduct; double myNet; double myGross; boolean [] trshIt = {myP, myPla, myGl, myCa}; double [] CO2TrashEmissions = {184.0, 25.6, 46.6, 165.8}; //constructor CO2FromWaste(int crust, boolean p, boolean pl, boolean gl, boolean ca) { myPlot = 1018.0; myCrust = crust; myP = p; myPl = pl; myGl = gl; myCa = ca; }
My issue is that boolean array, trshIt. Since I am putting variables in the array that have not been initialized yet, it gives those variables default values of false. If I put it in the constructor first, then I get an error complaining that the variable trshIt can not be found; pointing to the instance that I am calling that variable. So I have tried different forms of it in different areas and I am like trapped in a maze right now trying to get that array to work properly. I need all the help I can get. Ideas?
-
Admin about 12 yearsHmm, good try, although I have tried that and I get an 'illegal start of expression' error right before the first squiggly bracket on the trshIt array. And it's unclear to me why I get this.
-
Louis Wasserman about 12 yearsYou might need
trshIt = new boolean[] {myP, myPla, myGl, myCa};
-
scibuff about 12 yearsyou can use the shortcut array notation only when you declare the variable, ie you can do
boolean[] a = {true, false};
but notboolean[] a; a = {true, false};
-
Diego about 12 yearsOoops. Ok. Edited to fix that.
-
Admin about 12 yearsHmm, I did your style and I managed to get passed that first error. But running the script I now receive an error that trshIt is null when I ask for the length of it in a for loop. Even looking at the value of it in the debugger it reports that it's null. I never had anything like that before. Thoughts? Thanks for helping me through error 1 by the way.
-
scibuff about 12 yearsIt can be null only if this specific constructor is not called. Do you have more than one constructor or do you use
new CO2FromWaste()
instead ofnew CO2FromWaste( crust, p, pl, gl, ca )
anywhere? -
Admin about 12 yearsI only have one constructor, and I used new CO2FromWaste with the parameters several times as objects in an arraylist.
-
Skip Head about 12 years@Michael: You should accept this answer, and post another question with the code that is causing the new problem.
-
Admin about 12 yearsIT WORKED! Although I had to change boolean [] trshIt; to boolean [] trshIt = new boolean [4]; and added what you put in by assigning each index the variable in the constructor. Finally getting the magic on the terminal.
-
Admin about 12 yearsDon't worry, It's figure out now. Thank you user1000959. (can't do the useful answer thing yet)