1

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
    }
}

}

asdfajigs
  • 13
  • 5
  • 3
    Wow, you should have 100 different cars to learn what is code reuse. 6 was not enough as I see – AdamSkywalker May 01 '16 at 18:33
  • 2
    Possible duplicate of [Division of integers in Java](http://stackoverflow.com/questions/7220681/division-of-integers-in-java) – resueman May 01 '16 at 18:33
  • @resueman just not adding 1 to 0 at the for loop, and at the main method. I dont think its dividing. Supposed to keep adding 1 every-time different car is used – asdfajigs May 01 '16 at 18:40

2 Answers2

1

because carsValues[0] is never increased and yet you print only carsValues[0] every time. look at the break in the for loop:

for(int counter = 0; counter < input.length; counter++){
        JOptionPane.showMessageDialog(null, "Amount of Times Chosen"
                + "\n"+(input[counter]));
        break;// bad idea
}
0

In your example, the last loop:

for(int counter = 0; counter < input.length; counter++){
    JOptionPane.showMessageDialog(null, "Amount of Times Chosen"
            + "\n"+(input[counter]));
    break;// bad idea

The loop breaks after 0 everytime, and carsValues[0] is never used. So your result will always be 0.

Here's a way that would work. It can be refactored depending on your skill level.

import javax.swing.JOptionPane;
public class HelloWorld
{
  public static void main(String[] args) {
      int prompt;
      int[] carsValues = new int[6];//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 < 0 || prompt > 6){
            JOptionPane.showMessageDialog(null, "Unrecognizable Command"
                                                + "\n"
                                                + "Error: Entered Option must be between 1 and 5 inclusive"
                                                + "\n"
                                                + "Try Again"
                                                + "\n");
          }
          else{
              int n = prompt;
              carsValues[n]++;
              display(carsValues);
          }
      }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");   
          }
          JOptionPane.showMessageDialog(null, "Amount of Times Chosen"
                + "\n"
              + "Honda: " + input[1]
              + "\n"
              + "Toyota: " + input[2]
              + "\n"
              + "Ford: "  + input[3]
              + "\n"
              + "Chevrolet: " + input[4]
              + "\n"
              + "Kia: " + input[5]
              + "\n");
  }
}
Eugene
  • 10,957
  • 20
  • 69
  • 97