Results 1 to 14 of 14
Thread: About arrays
- 03-07-2011, 01:25 PM #1
Member
- Join Date
- Mar 2011
- Posts
- 7
- Rep Power
- 0
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]);
}
}
}
- 03-07-2011, 01:43 PM #2
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!
- 03-07-2011, 02:07 PM #3
Member
- Join Date
- Mar 2011
- Posts
- 7
- Rep Power
- 0
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 !!
- 03-07-2011, 02:12 PM #4
How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
- 03-07-2011, 02:24 PM #5
Member
- Join Date
- Mar 2011
- Posts
- 7
- Rep Power
- 0
- 03-07-2011, 02:35 PM #6
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!
- 03-07-2011, 02:44 PM #7
Member
- Join Date
- Mar 2011
- Posts
- 7
- Rep Power
- 0
- 03-07-2011, 02:56 PM #8
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!
- 03-07-2011, 02:57 PM #9
Senior Member
- Join Date
- Mar 2010
- Posts
- 953
- Rep Power
- 4
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-
- 03-07-2011, 03:00 PM #10
Member
- Join Date
- Mar 2011
- Posts
- 7
- Rep Power
- 0
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 :)
- 03-07-2011, 03:04 PM #11
Senior Member
- Join Date
- Mar 2010
- Posts
- 953
- Rep Power
- 4
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-
- 03-07-2011, 03:05 PM #12
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!
- 03-07-2011, 03:29 PM #13
Member
- Join Date
- Mar 2011
- Posts
- 7
- Rep Power
- 0
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 :))
- 03-07-2011, 03:35 PM #14
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
-
Need Help with Arrays!
By kassoli in forum New To JavaReplies: 3Last Post: 06-03-2010, 05:19 AM -
Help with arrays please.
By ThrashingBoy in forum New To JavaReplies: 2Last Post: 05-05-2010, 12:47 AM -
store array of arrays in array of arrays
By joost_m in forum New To JavaReplies: 4Last Post: 04-19-2010, 10:32 AM -
Arrays.sort... why sorting all arrays in class?
By innspiron in forum New To JavaReplies: 6Last Post: 03-23-2010, 01:40 AM -
arrays
By hasysf in forum New To JavaReplies: 12Last Post: 07-28-2008, 02:38 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks