0

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[][];
}
}
asked Dec 12, 2015 at 20:14
4
  • 1
    It just looks like you don't want "multidimensional arrays" here but actual POJOs. Commented 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 out Commented Dec 12, 2015 at 20:19
  • 1
    What 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? Commented 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. Commented Dec 12, 2015 at 20:22

1 Answer 1

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[][]); 
marc_s
760k186 gold badges1.4k silver badges1.5k bronze badges
answered Dec 12, 2015 at 20:24
Sign up to request clarification or add additional context in comments.

8 Comments

interesting. how do I ask the user to enter the column and row they want? within a different method?
I have to use JOptionPane only, and for loops basically. Only one class. They deliberately made this hard for me :(
showInputDialogue is a method of JOPtionPane :) I imagine they must mean for you to use it
thanks! i'm trying to create a menu like this: puu.sh/lT5r5/4b7fd3262d.png
ok edited again. You should also maybe think about storing your values differently. You have the first array element storing your types and the others storing an option for each type at the corresponding element of each array. It would be better to have all of one type in a single element. so they are grouped and you only have to iterate through a sub element when dealing with a given type
|

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.