Results 1 to 14 of 14

Thread: About arrays

  1. #1
    jezer08 is offline Member
    Join Date
    Mar 2011
    Posts
    7
    Rep Power
    0

    Default About arrays

    Im trying to add a new item in an existing list of menu. and the program will keep asking if you would like to add a new item in the menu. until i input a cue that it will stop asking.

    ok so I try to do that. so I paste here my code..

    if you input a "space" on the prompt "Enter your new menu" the loop will be terminated.

    but i have a problem whenever i input an another menu
    the arrays was just being updated and not adding a new menu..

    for example I input a "bacon" and i put my desire prices on it.
    however, if the program loops again and this time I input "Burger" and i put my desire prices on it.when the list is printed out again. the "bacon" was just being replaced by the "burger". how could I actually add a new item?

    I Hope you get my problem fellas :D pls help mee



    import java.io.*;
    public class menu {


    public static void main (String [] args)throws IOException{
    BufferedReader read=new BufferedReader(new InputStreamReader(System.in));



    String[] newMenu=new String[100];
    String[] newHalf=new String[100];
    String[] newWhole=new String[100];

    for(int i=0;;i++){

    System.out.println("Enter your new menu input<Space> if your done");
    newMenu[i]=read.readLine();
    if(newMenu[i].equals(" "))
    {break;}
    System.out.println("Enter its Half Order");
    newHalf[i]=read.readLine();
    System.out.println("Enter its Whole Order");
    newWhole[i]=read.readLine();

    System.out.println("==== Menu List ====\n");
    System.out.println("Beef Broccoli");
    System.out.println("Whole:210 Half:185");
    System.out.println("Stir Fried Beef");
    System.out.println("Whole:245 Half:195");
    System.out.println("Beef Stew");
    System.out.println("Whole:215 Half:145 ");
    System.out.println("Sweet and Sour Pork");
    System.out.println("Whole:266 Half:155 ");
    System.out.println("Crispy Pata");
    System.out.println(newMenu[i]);
    System.out.println("Whole:"+newWhole[i]+"Half:"+newHalf[i]);


    }
    }
    }

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,041
    Rep Power
    10

    Default

    The problem is that you only ever print out one item from the array- the last value entered. You aren't replacing the old values, you simply aren't printing them out. To see what I mean, print out the value of i when you print out the menu.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    jezer08 is offline Member
    Join Date
    Mar 2011
    Posts
    7
    Rep Power
    0

    Default

    Quote Originally Posted by KevinWorkman View Post
    The problem is that you only ever print out one item from the array- the last value entered. You aren't replacing the old values, you simply aren't printing them out. To see what I mean, print out the value of i when you print out the menu.

    hmm sir what I am trying to do is to add items until the user is satisfied. but not exceeding 100 items.

    is there any technique?

    sorry Im a noob please bear with me.

    sir just wondering if you could quote my codes and correct it ? will you? plss. :) hehe.

    thanks you sir !!

  4. #4
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,041
    Rep Power
    10

    Default

    Quote Originally Posted by jezer08 View Post
    hmm sir what I am trying to do is to add items until the user is satisfied. but not exceeding 100 items.
    Okay. Where is the code that makes sure the index does not exceed 100?

    Quote Originally Posted by jezer08 View Post
    sir just wondering if you could quote my codes and correct it ? will you? plss. :) hehe.
    Absolutely not. That's not how this works. I told you the problem. If you ask for code again, I will not help you further.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    jezer08 is offline Member
    Join Date
    Mar 2011
    Posts
    7
    Rep Power
    0

    Default

    Quote Originally Posted by KevinWorkman View Post
    Okay. Where is the code that makes sure the index does not exceed 100?
    String[] newMenu=new String[100];
    String[] newHalf=new String[100];
    String[] newWhole=new String[100];

  6. #6
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,041
    Rep Power
    10

    Default

    No. That creates arrays with 100 indexes. Where do you make sure your variable i does not exceed 100 (or 99)?

    And that's not even your most basic problem. Like I said, you are only printing out the current value of i, not any of the previous values. So you're only printing the most recently added menu item.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  7. #7
    jezer08 is offline Member
    Join Date
    Mar 2011
    Posts
    7
    Rep Power
    0

    Default

    Quote Originally Posted by KevinWorkman View Post
    No. That creates arrays with 100 indexes. Where do you make sure your variable i does not exceed 100 (or 99)?
    yeah i should have put it in for(int i=0;i<=100;i++).. my bad
    Quote Originally Posted by KevinWorkman View Post
    And that's not even your most basic problem. Like I said, you are only printing out the current value of i, not any of the previous values. So you're only printing the most recently added menu item.
    yes that exactly my problem. i dont have any idea on how to preserve the latter added items T_T

  8. #8
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,041
    Rep Power
    10

    Default

    The thing is, they ARE preserved. I don't think you're listening to me. You simply are not printing them out.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  9. #9
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    952
    Rep Power
    5

    Default

    Quote Originally Posted by jezer08 View Post
    yes that exactly my problem. i dont have any idea on how to preserve the latter added items T_T
    You are preserving them. You are not printing them. This is what Kevin has told you twice. Did you try what he suggested and print out the value of i along with the menu item? That should help you understand what is going wrong.

    Two suggestions that may help:

    1) Make your arrays instance variables.

    2) Put your menu printing code into a printMenu() method.

    Also, when you post code on the forum, please use CODE tags.

    -Gary-

  10. #10
    jezer08 is offline Member
    Join Date
    Mar 2011
    Posts
    7
    Rep Power
    0

    Default

    I try to print them out...

    System.out.println(newMenu[i]);
    System.out.println("Whole:"+newWhole[i]+"Half:"+newHalf[i]);

    if this is not what you are saying.. I guess i dont know how to do it right...if you could just teach me then. it will be awesome :)

  11. #11
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    952
    Rep Power
    5

    Default

    We didn't miss those two lines. We saw them. They will print out one menu item -- the one with an index of i. You want to print out more than one -- you want to print out menu item 0, menu item 1, and all the others up to i. You are not doing that.

    -Gary-

  12. #12
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,041
    Rep Power
    10

    Default

    What is the value of i each time you print? I'm not sure what else I can say to make it more clear.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  13. #13
    jezer08 is offline Member
    Join Date
    Mar 2011
    Posts
    7
    Rep Power
    0

    Default

    haha. i feel really st*pid god damm*t :)) I really dont know how to print them.. anyway just gonna sleep for now :D tnx for your time.. hopefully i could figure that out in my dreams :))

  14. #14
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,041
    Rep Power
    10

    Default

    Hint: write a method that prints out the entire array. Call that method when you want to print it.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Need Help with Arrays!
    By kassoli in forum New To Java
    Replies: 3
    Last Post: 06-03-2010, 06:19 AM
  2. Help with arrays please.
    By ThrashingBoy in forum New To Java
    Replies: 2
    Last Post: 05-05-2010, 01:47 AM
  3. store array of arrays in array of arrays
    By joost_m in forum New To Java
    Replies: 4
    Last Post: 04-19-2010, 11:32 AM
  4. Arrays.sort... why sorting all arrays in class?
    By innspiron in forum New To Java
    Replies: 6
    Last Post: 03-23-2010, 02:40 AM
  5. arrays
    By hasysf in forum New To Java
    Replies: 12
    Last Post: 07-28-2008, 03:38 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •