For this I wanted to print out the frequencies of the amount of times each types of car was chosen... however, as I run the program, its printing out a 0 (for frequency) each time I chose honda for example. It would be better to print out a table of all frequencies at the end of each program, however I'm just not sure how to get there.
public static void main(String[] args) {
int prompt;
int[] carsValues = new int[5];//didnt want to use 0
do{
prompt = Integer.parseInt(JOptionPane.showInputDialog(null, "Enter"
+ "\n"
+ "1 For Honda"
+ "\n"
+ "2 For Toyota"
+ "\n"
+ "3 For Ford"
+ "\n"
+ "4 For Chevrolet"
+ "\n"
+ "5 For Kia"
+ "\n"
+ "6 To Quit"));
if (prompt == 1)
{
display(carsValues);
int n = 1;
carsValues[n]++;
display(carsValues);
};
if (prompt == 2)
{
display(carsValues);
int n = 2;
carsValues[n]++;
display(carsValues);
};
if (prompt == 3)
{
display(carsValues);
int n = 3;
carsValues[n]++;
display(carsValues);
};
if (prompt == 4)
{
display(carsValues);
int n = 4;
carsValues[n]++;
display(carsValues);
};
if (prompt ==5)
{
display(carsValues);
int n = 5;
carsValues[n]++;
display(carsValues);
}
if (prompt >= 7)
{
JOptionPane.showMessageDialog(null, "Unrecognizable Command"
+ "\n"
+ "Error: Entered Option Is Greater Than The Choice of 5"
+ "\n"
+ "Try Again"
+ "\n");
};
if (prompt <= 0)//child proofing
{
JOptionPane.showMessageDialog(null, "Unrecognizable Command"
+ "\n"
+ "Error: Entered Option Is A 0 Or A Negative"
+ "\n"
+ "Try Again"
+ "\n");
};
}while(prompt!= 6);
}
public static void display(int[] input){
try{
int miles, gallons, mpg;
miles = Integer.parseInt(JOptionPane.showInputDialog(null,"Enter Miles Driven "));
if (miles <= -1){
JOptionPane.showMessageDialog(null,"Input Is Negative"
+ "\n"
+ "Try Again");
miles = Integer.parseInt(JOptionPane.showInputDialog(null,"Enter Miles Driven "));
}
gallons = Integer.parseInt(JOptionPane.showInputDialog(null,"Enter Gallons Used "));
if (gallons <= -1){
JOptionPane.showMessageDialog(null,"Input Is Negative"
+ "\n"
+ "Try Again");
gallons = Integer.parseInt(JOptionPane.showInputDialog(null,"Enter Gallons Used "));
}
mpg = miles/gallons;
JOptionPane.showMessageDialog(null, String.format("MPG Turns Out To Be %n" + mpg));
}catch(ArithmeticException mathError){
JOptionPane.showMessageDialog(null, "Division by Zero"
+ "\n"
+ "Can't Do That");
}
for(int counter = 0; counter < input.length; counter++){
JOptionPane.showMessageDialog(null, "Amount of Times Chosen"
+ "\n"+(input[counter]));
break;// bad idea
}
}
}