1

This is a pretty strange problem.

For some reason when my loop is iterated the first time, it outputs an extra line to the user.

Here is my code:

import java.util.Scanner;   
public class FunRentals {
    public static void main(String[] args) {
        Scanner new_scan = new Scanner(System.in);  
        System.out.println("Enter the amount of customers: ");
        int num_customers = new_scan.nextInt();
        double counter = 0;
        int x = 1;

        while(x<= num_customers){
            double sum = 0;

            System.out.print("Please enter the service used (\"Clowns\", \"Safari\", or \"Caravan\") for customer #"+x);
            String service_type = new_scan.nextLine();
            String service_type_real = service_type.toLowerCase();



            if(service_type_real.equals("clowns")){ 
                System.out.println("Please enter the amount of ADDITONAL hours");   
                double additional_hours = new_scan.nextDouble();

                sum = clowns(additional_hours);                                     
                System.out.println("The total bill for customer #" +x +" is "+ clowns(additional_hours));
            } 
            if(service_type_real.equals("safari sam")){
                System.out.println("Please enter the amount of ADDITONAL hours");   
                double additional_hours = new_scan.nextDouble();
                sum = safari_sam(additional_hours);                                 
                System.out.println("The total bill for customer #" +x +" is "+ safari_sam(additional_hours));
            } 
            if(service_type_real.equals("music caravan")){
                System.out.println("Please enter the amount of ADDITONAL hours");   
                double additional_hours = new_scan.nextDouble();
                sum = music_caravan(additional_hours);
                System.out.println("The total bill for customer #" +x +" is "+ sum);
            } 

            counter = counter + sum; x++;
        }
        System.out.println("The total amount of money made was $"+counter);
    }

    public static double clowns(double a){
        double additional_cost = a*35;
        double total_cost = additional_cost + 45;
        return total_cost;
    }

    public static double safari_sam(double a){
        double additional_cost = a*45;
        double total_cost = additional_cost + 55;
        return total_cost;
    }
    public static double music_caravan(double a){
        double additional_cost = a*30;
        double total_cost = additional_cost + 40;
        return total_cost;
    }
}

Question: Does anybody know why System.out.print("Please enter the service used (\"Clowns\", \"Safari\", or \"Caravan\") for customer #"+x); Outputs TWICE instead of once in the first iteration? I need to pass this in by tomorrow and don't know why the loop isn't working properly and running the code once per iteration.

Cruncher
  • 7,241
  • 1
  • 26
  • 62
Robert Tossly
  • 603
  • 5
  • 14

0 Answers0