Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 19 of 19

Thread: Arrays being initialized from another class

  1. #1
    Junior Member
    Join Date
    Jan 2012
    Posts
    9
    My Mood
    Confused
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Question 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."

    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;
            }
        }
    }

    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?


  2. #2
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default 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)

  3. #3
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default 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.

  4. #4
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: Arrays being initialized from another class

    To your current code: What is payroll?
    oh i didnt notice that very carefully, -_-, i just assume that he got an initializing problem.

  5. #5
    Junior Member
    Join Date
    Jan 2012
    Posts
    9
    My Mood
    Confused
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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:

    //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

     
    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?
    Last edited by Java Man 9000; January 11th, 2012 at 12:49 PM.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Arrays being initialized from another class

    Do you have two variables named: storeID?
    That can be confusing

  7. #7
    Junior Member
    Join Date
    Jan 2012
    Posts
    9
    My Mood
    Confused
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Arrays being initialized from another class

    Quote Originally Posted by Norm View Post
    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.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default 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?

  9. #9
    Junior Member
    Join Date
    Jan 2012
    Posts
    9
    My Mood
    Confused
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Arrays being initialized from another class

    Quote Originally Posted by Norm View Post
    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).

    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
    }

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default 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.
    Last edited by Norm; January 11th, 2012 at 01:11 PM.

  11. #11
    Junior Member
    Join Date
    Jan 2012
    Posts
    9
    My Mood
    Confused
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Arrays being initialized from another class

    Quote Originally Posted by Norm View Post
    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.
    Last edited by Java Man 9000; January 11th, 2012 at 01:38 PM. Reason: Missed last comment's question

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Arrays being initialized from another class

    If there is no confusion with having the same name, then leave it.

  13. #13
    Junior Member
    Join Date
    Jan 2012
    Posts
    9
    My Mood
    Confused
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Arrays being initialized from another class

    Quote Originally Posted by Norm View Post
    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.

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Arrays being initialized from another class

    the code won't let me
    Please post the code and the error messages.

    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.

  15. #15
    Junior Member
    Join Date
    Jan 2012
    Posts
    9
    My Mood
    Confused
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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

    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?
    Last edited by Java Man 9000; January 11th, 2012 at 01:39 PM.

  16. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Arrays being initialized from another class

    Where is the variable: employeeID used in the code you just posted???

  17. #17
    Junior Member
    Join Date
    Jan 2012
    Posts
    9
    My Mood
    Confused
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Arrays being initialized from another class

    Sorry Norm employeeID is on another part of my outlined program my latter post has been changed.

  18. #18
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default 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.

  19. The Following User Says Thank You to Norm For This Useful Post:

    Java Man 9000 (January 11th, 2012)

  20. #19
    Junior Member
    Join Date
    Jan 2012
    Posts
    9
    My Mood
    Confused
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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.

Similar Threads

  1. How to Sort an Array using the java.util.Arrays class
    By JavaPF in forum Java SE API Tutorials
    Replies: 2
    Last Post: May 17th, 2014, 01:16 AM
  2. [Help] Problem with Class arrays, adding/editing and deleting
    By Grant_Searle in forum Object Oriented Programming
    Replies: 7
    Last Post: December 16th, 2011, 10:10 AM
  3. [SOLVED] Writing a program with arrays and class methods... PLEASE HELP?
    By syang in forum What's Wrong With My Code?
    Replies: 17
    Last Post: August 8th, 2011, 07:02 AM
  4. Issue with setting up different class with arrays
    By D-COD3R in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 4th, 2011, 06:09 PM
  5. How to Sort an Array using the java.util.Arrays class
    By JavaPF in forum Java Code Snippets and Tutorials
    Replies: 0
    Last Post: December 1st, 2008, 09:02 AM

Tags for this Thread