Arrays being initialized from another class
Hey, my program is supposed to display stores, their unique identification number, and their income. I realize that my code doesn't accurately demonstrate the program intended.
My test code says that the error is in bold saying that "the variable storeID might not have been initialized."
Code Java:
import javax.swing.JOptionPane;
import java.text.DecimalFormat;
import java.text.NumberFormat;
public class store
{
public static void main(String[] args)
{
payroll storeID[];
DecimalFormat df = new DecimalFormat("#,##0.00");
NumberFormat nf = NumberFormat.getCurrencyInstance();
double income;
byte currentStore;
currentStore = 0;
//Used to store the data in the arrays in the future.
while(currentStore <= 7)
{
JOptionPane.showMessageDialog(null,
[B]storeID[currentStore],[/B]
"Store " + storeID[currentStore],
JOptionPane.PLAIN_MESSAGE);
currentStore += 1;
}
}
}
Code Java:
public class storeSource
{
//Number of stores
public final int num_stores = 7;
//Creates an array to hold the store's identification number.
private int[] storeID = {5658845, 4520125, 7895122, 8777541, 8451277, 1302850, 7580489};
//Creates an array to hold the total income that each store earns.
private double[] wages = new double[num_stores];
}
Can I gain some insight with this?
Re: Arrays being initialized from another class
local variables are different from data members(or Global variables), a local variable should be initialized before you use it else where inside methods, so the compiler will have the idea what are their default values.
storeID[] - this is an array not just an ordinary variable or identifier, the compiler can not understand its size or its contents when you use it somewhere(i.e inside your while loop)
Re: Arrays being initialized from another class
The code you've provided is not sufficient to draw any conclusion, what's wrong. Post the SSCCE
To your current code: What is payroll?
And you can assign values to an array but not the object that expects array to be created but not yet created.
Re: Arrays being initialized from another class
Quote:
To your current code: What is payroll?
oh i didnt notice that very carefully, -_-, i just assume that he got an initializing problem. :(
Re: Arrays being initialized from another class
Sorry the payroll was supposed to be storeSource. Is this the source of my problem or is it still because my variable isn\'t identified locally in my "store" class?
Here\'s where my program is based off of:
Code Java:
//Introducing the main method and class
{
storeSource storeID[];
//Used to store the data in the arrays in the future.
while(currentStore <= 7)
{
JOptionPane.showMessageDialog(null,
[B]storeID[currentStore],[/B]
"Store " + storeID[currentStore],
JOptionPane.PLAIN_MESSAGE);
currentStore += 1;
}
}
}
The array is identified in this next class
Code Java:
public class storeSource
{
//Number of stores
//Creates an array to hold the store\'s identification number.
[B]private int[] storeID = {1, 2, 3, 4, 5, 6, 7};[/B]
//More code
}
If it is my storeID array not locally being initialized what would it look like?
Re: Arrays being initialized from another class
Do you have two variables named: storeID?
That can be confusing
Re: Arrays being initialized from another class
Quote:
Originally Posted by
Norm
Do you have two variables named: storeID?
That can be confusing
No I am trying to get my store.class to retrieve the data from the storeSource.class and when I take out "storeSource storeID;" the variable can't be found.
Re: Arrays being initialized from another class
What are each of the two arrays used for? Are they related in any way or are they completely independent of each other?
Re: Arrays being initialized from another class
Quote:
Originally Posted by
Norm
What are each of the two arrays used for? Are they related in any way or are they completely independent of each other?
The store.class file is supposed to refer to the storeSource.class file in order to later store data under other arrays. The storeID array below is to be referred to when creating arrays (to keep consistency).
Code Java:
public class storeSource
{
//Number of stores
//Creates an array to hold the store's identification number.
[B]private int[] storeID = {1, 2, 3, 4, 5, 6, 7};[/B]
//More code
}
Re: Arrays being initialized from another class
storeSource storeID[];
int[] storeID =
The first storeID array is a different type than the second one.
Can you explain your problem now and show the code where you are having problems. Also post any error messages that you are having a problem with.
Re: Arrays being initialized from another class
Quote:
Originally Posted by
Norm
storeSource storeID[];
int[] storeID =
The first storeID array is a different type than the second one.
Can you explain your problem now and show the code where you are having problems. Also post any error messages that you are having a problem with.
The only error that I have is: Error(25,17): variable storeID might not have been initialized
How can this be done then? The array isn't anything but "int" in the class files.
Re: Arrays being initialized from another class
If there is no confusion with having the same name, then leave it.
Re: Arrays being initialized from another class
Quote:
Originally Posted by
Norm
If there is no confusion with having the same name, then leave it.
There isn't confusion the code won't let me access the data from the array on the storeSource.class on the store.class.
Re: Arrays being initialized from another class
Quote:
the code won't let me
Please post the code and the error messages.
Quote:
access the data from the array on the storeSource.class on the store.class
For methods in the Store class to access data in the StoreSource class, you can add some methods to the StoreSource class that the Store class methods can call to get the data from the storeID array.
Re: Arrays being initialized from another class
Quote:
Originally Posted by Java Man 9000
The only error that I have is: Error(25,17): variable storeID might not have been initialized
The error is in this code
Code Java:
while(currentStore <= 7)
{
JOptionPane.showMessageDialog(null,
[B]storeID[currentStore],[/B]
"Store " + storeID[currentStore],
JOptionPane.PLAIN_MESSAGE);
currentStore += 1;
}
I am also new to calling from another arrays is this my problem to begin with?
Re: Arrays being initialized from another class
Where is the variable: employeeID used in the code you just posted???
Re: Arrays being initialized from another class
Sorry Norm employeeID is on another part of my outlined program my latter post has been changed.
Re: Arrays being initialized from another class
Where does the program assign a value to the storeID variable? The compiler thinks there is a way for it to not have a value.
Re: Arrays being initialized from another class
My problem is solved I only used one array and finally import the other class correctly. I will share my findings soon.