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 6 of 6

Thread: Filling an Array?

  1. #1
    Junior Member
    Join Date
    Oct 2009
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Filling an Array?

    Hey guys,

    I've come from another thread in hopes that someone can explain whether this is correct or not more simply (to get it through my java newbie brain without being too complicated for a newcomer).

    In the code below everything that is commented //add is by me. If you look right below that code at the TODO you'll see my instructions were to fill the menuDescriptions array with the item descriptions. I'm new at this but this program consists of 9 classes, if you need to see the rest of this class or other related classes to know what is going on please let me know.

    //method to return an array of descriptions
    	public String[] getMenuDescriptions(){
    		String menuDescription[] = new String[menuItems.length];
     
    		menuDescription[0] += menuItems[0].toString(); // add
            menuDescription[1] += menuItems[1].toString(); //add 
            menuDescription[2] += menuItems[2].toString(); //add
            menuDescription[3] += menuItems[3].toString(); //add
            menuDescription[4] += menuItems[4].toString();//add
            menuDescription[5] += menuItems[5].toString();//add
            menuDescription[6] += menuItems[6].toString();//add
            menuDescription[7] += menuItems[7].toString();//add
            menuDescription[8] += menuItems[8].toString();//add
     
    		//TODO
    		//FILL THE ARRAY menuDescription with the descriptions of the menu items
    		//return the menuDescription back to the calling method
     
     
    		return menuDescription;


    Here is a snippet of the same class with some code mentioned above to give you an idea of where menuItems came from:

    public Menu(){
    		menuItems = new Product[9];
     
    		Product book;
    		book = new Product("BK100", "Book", "Java", 12.99);
    		menuItems[0] = book;
    		book = new Product("BK200", "Book", "VB", 16.99);
    		menuItems[1] = book;
    		book = new Product("BK300", "Book", "PHP", 15.99);
    		menuItems[2] = book;

    Thanks, hope to make this my java program help forum!


  2. #2
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: Filling an Array?

    I'm not entirely sure what you need here, you seem to already populate the array with something from the toString method of your Product.

    Just use whatever appropriate method on the Product object you have to populate the array instead.

    menuDescription[0] += menuItems[0].getDescription(); // The method on your Product class which will give you the description.

    Or am I misunderstanding this?

    // Json

  3. #3
    Junior Member
    Join Date
    Oct 2009
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Filling an Array?

    Hmm well I wasn't sure if the code I added (with the //add comments) is correct are not. Someone was saying I should use a loop?

  4. #4
    Junior Member
    Join Date
    Oct 2009
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Filling an Array?

    I've solved this with some help using a for loop thanks guys.

  5. #5
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: Filling an Array?

    Well done, I shall mark this one as solved. Please share your solution if you don't mind. I'm sure other people will want to know as well

    // Json

  6. #6
    Junior Member
    Join Date
    Oct 2009
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Filling an Array?

    I ended up using this guys with some help from a tutor:

    public String[] getMenuDescriptions(){
    		String menuDescription[] = new String[menuItems.length];
     
     
    		for (int i = 0 ; i < menuDescription.length ; i++){
     
    			menuDescription[i] = menuItems[i].getDescription();
    			JOptionPane.showMessageDialog(null, menuItems[i].toString());
     
    		}
     
     
     
     
    		//TODO
    		//FILL THE ARRAY menuDescription with the descriptions of the menu items
    		//return the menuDescription back to the calling method
     
     
    		return menuDescription;
     
    	}

Similar Threads

  1. Storing an array into an array
    By vluong in forum Collections and Generics
    Replies: 4
    Last Post: September 22nd, 2009, 03:14 PM