Reputation: 111
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[][];
}
}
Upvotes: 0
Views: 68
Reputation: 420
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[][]);
Upvotes: 1