Question: This is what im told to do: It might not be reasonable to expect users to type long entries such as oil change accurately. Copy

This is what im told to do:
It might not be reasonable to expect users to type long entries such as "oil change" accurately. Copy the code from the CarCareChoice.java file into the CarCareChoice2.java file and modify the CarCareChoice class so that as long as the user enters the first three characters of a service correctly, the choice is considered valid. Rename the class CarCareChoice2.
This is what i have. The code works but only when i enter the first 3 letter. I want to be able to enter the whole word and if the first 3 letter are correct the code should work. I want to be able to enter more than just the first 3 letters of the word
This is the error i get:
Status: FAILED! Test: The `CarCareChoice2` program accepts misspelled selections. Reason: In this simulation, the input value was 'brack check'. The cost of a brake inspection is $5. Unable to find '5' in the program's output. Error : class java.lang.AssertionError
Here is my code:
import java.util.*;
public class CarCareChoice2{
public static void main(String args[]){
Scanner input = new Scanner(System.in);
String[] services ={"oil change", "tire rotation", "battery check", "brake inspection"};
double[] price ={25,22,15,5};
for (String service : services)
{
System.out.println("-"+ service);
}
System.out.println("Please enter the name of the service you would like:");
String serviceChoice = input.nextLine().toLowerCase();
boolean found = false;
for (int i =0; i < services.length; i++){
if (services[i].toLowerCase().startsWith(serviceChoice)){
System.out.println("You selected: "+ services[i]);
System.out.println(services[i]+" price is $"+ price[i]);
found = true;
break;
}
}
if (!found){
System.out.println("Invalid service selection. Please try again.");
}
input.close();
}
}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!