I'm trying to show the user a menu of wines to choose from. The wine types(Riesling, Chardonnay, etc) have wine variations. I want to show the user the entire wine menu, then they enter 1-4 to indicate which type they want, which shows them all of the rows for that column. Then, they can type 1-3 to select which row they want. My problem is, I don't know how to make a method for this. Also, the user can do this up to 16 times, or until they wish to quit. This is just one method, though. Anyone that can help would be greatly appreciated.
http://puu.sh/lT5r5/4b7fd3262d.png (Prompt)
import javax.swing.JOptionPane;
public class WineCalc{
public static void main(String[] args){
String[][]wineTypes = {
{"Riesling", "Chardonnay", "Sauvignon Blanc", "Merlot"},
{"Dry- 4ドル.50", "Apple- 6ドル.00", "Lime-4ドル.50", "Plum- 5ドル.00"},
{"Off Dry-4ドル.00", "Lemon-5ドル.50", "Lemongrass- 6ドル.50", "Black Cherry- 7ドル.50"},
{"Sweet- 5ドル.00", "Vanilla- 6ドル.00", "Coconut- 7ドル.00", "Chocolate- 6ドル.00"},
};
double[][]prices = {
{4.50, 6.00, 4.50, 5.00},
{4.00, 5.50, 6.50, 7.50},
{5.00, 6.00, 7.00, 6.00},
};
int[][]counter = {
{0,0,0,0},
{0,0,0,0},
{0,0,0,0},
};
}
public static String getWineType(String wineTypes[][]){
for(i=0; i<wineTypes[0].length;i++){
for(int j=0; j<wineTypes.length; j++){
JOptionPane.showMessageDialog(null, wineTypes[i][j]);
}
}
return wineTypes[][];
}
}
-
1It just looks like you don't want "multidimensional arrays" here but actual POJOs.fge– fge2015年12月12日 20:17:57 +00:00Commented Dec 12, 2015 at 20:17
-
I can't use objects, I must use simple for loops and stuff. this is for class :( im just stuck..I want to let the user enter numbers corresponding to the rows and columns. it seems so easy but i cant figure it outHello– Hello2015年12月12日 20:19:00 +00:00Commented Dec 12, 2015 at 20:19
-
1What do you mean, you must? Is this a school assignment and your teaching staff forced this unreasonable request on you? Of course, you do realize that in real life programming this would never happen, right?fge– fge2015年12月12日 20:21:32 +00:00Commented Dec 12, 2015 at 20:21
-
Yes, this is for class, its actually due tonight. My department has deliberately tried to make this as impossible as they can. I'm stuck, and I read about alternatives to the way they want me to code this, and it seems so much simpler. I just need a hand making this method to let the user choose.Hello– Hello2015年12月12日 20:22:56 +00:00Commented Dec 12, 2015 at 20:22
1 Answer 1
You are declaring your 2d arrays fine but your method needs reworking. You are giving it a string return type but not returning anything, change this to void.
To get input:
// somewhere before the getWineType() call
int i = Integer.parseInt(JOptionPane.showInputDialogue("Enter First number: ")
// if you want to list all the wines in the sub query at this point put a method here to do so
int j = Integer.parseInt(JOptionPane.showInputDialogue("Enter Second number: ")
JOptionPane.showMessageDialog(wineTypes[i][j]); //this will show the selected wine
As for your list wines method.
public static void listWineTypes(wineTypes[][]) { //void return
listarray[][] = wineTypes[][];
for(i=0; i<listarray.length;i++){ //change this
for(int j=0; j<listarray[i].length; j++){ //and this
JOptionPane.showMessageDialog(wineTypes[i][j]); //this will print out each wine in the list.
}
You also have to call the method somewhere:
getWineType(wineType[][]);