1

There is a list of services to choose from, I need the user to enter a service from the list and it outputs what they chose and the cost.

This is my code, it compiles correctly, I just don't understand why it keeps outputting invalid input every time when I type in exactly what is in the array into the input box.

Any ideas?

import javax.swing.*;
public class CarCareChoice
{
   public static void main(String[] args)
   {
      final int NUM_OF_ITEMS = 4;
      String[] validChoices = {"oil change", "tire rotation", "battery check", "brake inspection"};
      double[] prices = {25, 22, 15, 5};
      String strOptions;
      String careChoice;
      double choicePrice = 0.0;
      boolean validChoice = false;
      strOptions = JOptionPane.showInputDialog(null, "Please enter one of the following care options: oil change, tire rotation, battery check, or brake inspection");
      careChoice = strOptions;
      for(int x = 0; x < NUM_OF_ITEMS; ++x)
      {
         if(careChoice == validChoices[x])
         {
             validChoice = true;
             choicePrice = prices[x];
         }
     }
     if(validChoice)
        JOptionPane.showMessageDialog(null, "The price of a " + careChoice + " is $" + choicePrice);
     else
        JOptionPane.showMessageDialog(null, "Sorry - invalid entry");

   }
}
Alex204
  • 45
  • 8

3 Answers3

0

You can't compare strings with == in Java.

This will work though:

validChoices[x].equals(careChoice)
Alexey Soshin
  • 16,718
  • 2
  • 31
  • 40
0
import javax.swing.*;
public class CarCareChoice
{
   public static void main(String[] args)
   {

      // ...

      for(int x = 0; x < NUM_OF_ITEMS; ++x)
      {
         if(careChoice.equals(validChoices[x]))
         {
             validChoice = true;
             choicePrice = prices[x];
         }
     }
     if(validChoice)
        JOptionPane.showMessageDialog(null, "The price of a " + careChoice + " is $" + choicePrice);
     else
        JOptionPane.showMessageDialog(null, "Sorry - invalid entry");

   }
}

You cannot compare Strings with ==. Instead, you must use the String.equals() method.

Charles
  • 1,384
  • 11
  • 18
0

I hope this would be the best answer you were looking for.

import javax.swing.*;
public class CarCareChoice
{
   public static void main(String[] args)
   {
      final int NUM_OF_ITEMS = 4;
      String[] validChoices = {"oil change", "tire rotation", "battery check", "brake inspection"};
      double[] prices = {25, 22, 15, 5};
      String strOptions;
      int careChoice;
      double choicePrice = 0.0;
      boolean validChoice = false;
      strOptions = JOptionPane.showInputDialog(null, "Please enter one of the following care options: oil change, tire rotation, battery check, or brake inspection");
      careChoice = Integer.valueOf(strOptions);
      System.out.println("care choice :" + careChoice);
      if(careChoice < validChoices.length){
        validChoice = true;
        choicePrice = prices[careChoice-1];
      }
     if(validChoice)
        JOptionPane.showMessageDialog(null, "The price of a " + careChoice + " is $" + choicePrice);
     else
        JOptionPane.showMessageDialog(null, "Sorry - invalid entry");

   }
}

Hope this works :)

Manish Singh
  • 518
  • 3
  • 13