Kel Okekpe Kel Okekpe - 2 months ago 12
Java Question

User Input incorporating Arrays and While Loops

I am creating some code that displays an

of services and their corresponding prices to the user.

Whenever the user chooses a service, I want the price of that service to be displayed to the user...

If the user wishes for multiple services to be performed, I want the computer to ask them until they say otherwise.

Also, I need to use an accumulator to add the prices of the services that user requests.

So far, this is what I have:

import java.util.Scanner;
public class Assign3
public static void carMake(String bmw)
Scanner input = new Scanner(;

//declare and intialize parallel arrays, Services and Prices and display them to the user
String[] services = {"Oil Change" , "Tire Rotation", "Air Filter", "Fluid Check"}; //intialize list of services
double[]price = {39.99, 49.99, 19.99, 10.99}; //initialize corresponding price for services
for(int i= 0; i < services.length; i++)
System.out.print( services[i]+ "...." );
System.out.print( price[i] + "\t");


System.out.println("What service do you want done?: ");
String service= input.nextLine();

public static void main(String[]args)
String bmw = "fancy car";



Although I'm not submitting a full program, I can give you all the information you need to finish this project. First, you have to decide how to incorporate the connection between a service and it's price.

A better approach instead of two arrays would be to create a "Service" object. For example:

public class Service {
     // Variables to use
     String name = "";
     double price = 0.0;

     // Constructor of the 'Service' class
     public Service(String inputName, double inputPrice) {
          name = inputName;
          price = inputPrice;

This would allow you to then make the call:

// Create the oilChange service
Service oilChange = new Service("Oil Change", 39.99);

// Get the oilChange values:
System.out.println(; // Displays 'Oil Change'
System.out.println(oilChange.price); // Displays '39.99'

From there, you have to determine whether or not to use a for-loop or a while- loop. A for-loop requires you to know how many times the loop will run. However, for your example, this is something you won't know. So therefore, you will have to use a while-loop. An example of a while-loop for your program would look like:

double totalPrice = 0.0;
boolean userIsDone = false;
Service listOfServices = [] // Contains created service objects from above
while (userIsDone == false) {
     System.out.println("What service do you want done? (Type 'quit' to exit): ");
     String service= input.nextLine();
     if (input.equalsIgnoreCase("quit")) {
          userIsDone = true;
     } else {
           1.) Loop through the 'listOfServices' array finding 
               a service with the name that matches user input
           2.) Then retrieve the price of that service and add 
               it to the 'totalPrice' counter
System.out.println(totalPrice); // Updated total price!

Hope this helps!