Skip to content
Advertisement

Not able to print the first element in an array (Inputed by user)

I am trying to print the types of vehicles that are being rented. The issue I’m currently having is that the print statement only prints the second type of input from the user.

Here I have the do loop where the user inputs the number of vehicles that they are renting. They rent less than 3 vehicles, which is what IsValidVehicleNumber(numOfVehicles) checks for. I created a new array that will then store what was inputed (either motorbike, car, or trailer). That array is vehicleTypes[]. I created ANOTHER array that store the properties of each of those vehicles inputted.

//Ask user how many vehicles are being rented
    do {
        System.out.println("Number of vehicles you are renting: ");
        numOfVehicles = ReadIntegerFromUser();
    }while(IsValidVehicleNumber(numOfVehicles) == false);

    //Here is where we begin the final part of the program:
    
    vehicleTypes = new String[numOfVehicles];
    vehicleRentedArry = new CVehicle[numOfVehicles];

I think this part makes sense. So, just to walk through some of it…. this will take the inputs and place them into the vehicleTypes array if they equal certain inputs.

    for(int i = 0; i < vehicleTypes.length; i++) {
        System.out.println("What vehicle type would you like? Motorbike, trailer, or car?: ");
        vehicleType = ReadStringFromUser();
        if((vehicleType.equalsIgnoreCase("Motorbike")) || (vehicleType.equalsIgnoreCase("Trailer")) || (vehicleType.equalsIgnoreCase("Car"))) {
            vehicleTypes[i] = vehicleType;
            for(int j = 0; j < vehicleRentedArry.length; j++) {
                if(vehicleType.equalsIgnoreCase("Motorbike")) {
                    CMotorbike motorbikeOption = new CMotorbike();
                    motorbikeOption.SetMethods("Handle Bars", 35, 2);
                    vehicleRentedArry[j] = motorbikeOption;
                }else if(vehicleType.equalsIgnoreCase("car")) {
                    CCar carOption = new CCar();
                    carOption.SetMethods("Steering Wheel", 25, 4);
                    vehicleRentedArry[j] = carOption;
                }else if(vehicleType.equalsIgnoreCase("Trailer")) {
                    CTrailer trailerOption = new CTrailer();
                    trailerOption.SetMethods("Use Another Vehicle", 0, 6);
                    vehicleRentedArry[j] = trailerOption;
            }
        
        }
    }
    }
    
    
    //Calculate the rates
    dailyRates = CalculateDailyRates(vehicleTypes, numOfVehicles, numOfDays);
    
    //Check if rates appear correctly
    
    for(int k = 0; k < dailyRates.length; k++) {
        totalRentalSale += dailyRates[k];
        //Adding tax to that:
        totalRentalSale = totalRentalSale *1.06f;

    }

This is the print part of the project. The concern is specifically the actual result I get.

    //Printing the outputs:
    System.out.println("** Order Confirmation **");
    System.out.printf("----------------------------------------------------------------------n");
    System.out.printf("Customer Name: tttt%s %sn", strFirstName, strLastName);
    System.out.printf("Phone Number: tttt%sn", strPhoneNumber);
    System.out.printf("Email: ttttt%sn", strEmail);
    System.out.printf("----------------------------------------------------------------------n");
    System.out.println("** Details about Rental **");
    System.out.printf("----------------------------------------------------------------------n");
    System.out.printf("Type of Vehicle: ttt");
    for(int intIndex = 0; intIndex < vehicleRentedArry.length; intIndex++) {
        System.out.println("Try this: " + vehicleRentedArry[intIndex].getVehicleType());
            
    }

HERE IS WHERE I HAVE A PROBLEM******* For the vehicle type, it is only printing the second input and not the first AND second. I want it to have the first input on the same line as “type of vehicle” and the second input below the first. To be clear, this is what is shown in my console.

Let's see if your inputs are valid!
-----------------------------------------------
Enter QUIT at anytime to exit.

First Name of Renter: John
Last Name of Renter: Smith
What phone number is best to get in contact? Use format '###-###-####' or '##########': 123-456-7890
Renter's Email: smithj@mail.com
When would you like to pick-up the vehicle? Use format MM-DD-YYYY or MM/DD/YYYY: 12-12-2021
Number of days you are renting: 1
Number of vehicles you are renting: 
2
What vehicle type would you like? Motorbike, trailer, or car?: 
Car
What vehicle type would you like? Motorbike, trailer, or car?: 
Trailer
** Order Confirmation **
----------------------------------------------------------------------
Customer Name:              John Smith
Phone Number:               123-456-7890
Email:                  smithj@mail.com
----------------------------------------------------------------------
** Details about Rental **
----------------------------------------------------------------------
Type of Vehicle:            Try this: Trailer
Try this: Trailer

Advertisement

Answer

It’s just that when the iteration of the outer loop is executed, the inner loop puts current type into every cell in the vehicleRentedArry, which results in all of them being the same at the end – the last input.

It looks like the inner loop should be just an “if-else”, or the loops should be separate (the inner should be “pulled out”, executed after the outer) or — you can see i’m not entirely sure what’s going on — the vehicleType should be gotten from corresponding (j-th) cell of the vehicleType array each iteration. In any case, if you want to rent up to 3 vehicles and let the user decide type of each (which I believe you do), it’s clear that the vehicleType should not be the same in every iteration of the inner loop.

I’m pretty sure, that’s where the problem is. I suspect the following should work as you intend :

for(int i = 0; i < vehicleTypes.length; i++) {
System.out.println("What vehicle type would you like? Motorbike, trailer, or car?: ");
        vehicleType = ReadStringFromUser();
        if((vehicleType.equalsIgnoreCase("Motorbike")) || (vehicleType.equalsIgnoreCase("Trailer")) || (vehicleType.equalsIgnoreCase("Car"))) {
            vehicleTypes[i] = vehicleType;
                if(vehicleType.equalsIgnoreCase("Motorbike")) {
                    CMotorbike motorbikeOption = new CMotorbike();
                    motorbikeOption.SetMethods("Handle Bars", 35, 2);
                    vehicleRentedArry[i] = motorbikeOption;
                }else if(vehicleType.equalsIgnoreCase("car")) {
                    CCar carOption = new CCar();
                    carOption.SetMethods("Steering Wheel", 25, 4);
                    vehicleRentedArry[i] = carOption;
                }else if(vehicleType.equalsIgnoreCase("Trailer")) {
                    CTrailer trailerOption = new CTrailer();
                    trailerOption.SetMethods("Use Another Vehicle", 0, 6);
                    vehicleRentedArry[i] = trailerOption;
            }
    }
}
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement