Search code examples
javamethodssystem

Program randomly terminates after executing toUpperCase method in Java


My Java program terminates after using the System.out.println();

I have put System.out.println in a few places in my code to find out exactly where it is terminating and it seems to be terminating right after it executes the println

package exercises;
import java.util.Scanner;

public class TrainSeatBookingApplication {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        SeatType theSeatType;
        FloorGrid floorType;
        TrainWay aTrainWay = null;
        TrainSmart aTrainSmart = null;
        Seat customerSeat;
        char planeSizeChoice;
        char seatingArea;
        char seatEconomyOrFirst;
        char programBookingChoice;

        Scanner scan = new Scanner(System.in);
        System.out.println("Would you like to board a petite floor sized plane or a grande floor sized plane?");
        planeSizeChoice = scan.next().charAt(0);
        planeSizeChoice = Character.toUpperCase(planeSizeChoice);
        if (planeSizeChoice == 'P') {
            floorType = new PetiteFloorGrid();
            floorType.initialiseFloorGrid();
            System.out.println("Would you like to be in the middle, window or asile?");
            seatingArea= scan.next().charAt(0);
            seatingArea = Character.toUpperCase(seatingArea);
            System.out.println("Would you like to be seated in first class or middle class?");
            seatEconomyOrFirst = scan.next().charAt(0);
            seatEconomyOrFirst = Character.toUpperCase(seatingArea);
            System.out.println("Would you like your seat to be booked via the smart program or the way program?");
            programBookingChoice = scan.next().charAt(0);
            programBookingChoice = Character.toUpperCase(programBookingChoice);
            if (seatEconomyOrFirst == 'F') {
                    if (programBookingChoice == 'S') {
                        customerSeat =  aTrainSmart.reserveFirstClass(planeSizeChoice, SeatType.MIDDLE);
                        System.out.println(floorType);
                    }
                    else {
                        customerSeat =  aTrainWay.reserveFirstClass(planeSizeChoice, SeatType.MIDDLE);
                        System.out.println(floorType);
                    }
                }
            }
        else {
            floorType = new GrandeFloorGrid();
            floorType.initialiseFloorGrid();
            System.out.println("Would you like to be in the middle, window or asile?");
            seatingArea= scan.next().charAt(0);
            seatingArea = Character.toUpperCase(seatingArea);
            System.out.println("Would you like to be seated in first class or middle class?");
            seatEconomyOrFirst = scan.next().charAt(0);
            seatEconomyOrFirst = Character.toUpperCase(seatingArea);
            System.out.println("Would you like your seat to be booked via the smart program or the way program?");
            programBookingChoice = scan.next().charAt(0);
            programBookingChoice = Character.toUpperCase(programBookingChoice);
            System.out.println("Did not reach start of if");//testing program LINE57
            if (seatEconomyOrFirst == 'F') {
                if (programBookingChoice == 'S') {
                    customerSeat =  aTrainSmart.reserveFirstClass(planeSizeChoice, SeatType.MIDDLE);
                    System.out.println(floorType);
                }
                else {
                    customerSeat =  aTrainWay.reserveFirstClass(planeSizeChoice, SeatType.MIDDLE);
                    System.out.println(floorType);
                }
                //System.out.println("Did not go through either if or else");//testing program
            }
        }

    }
}

I have 2 other lines that do the exact same (but save to different variables) and they work perfectly fine.


Solution

  • This

    seatEconomyOrFirst = scan.next().charAt(0);
    seatEconomyOrFirst = Character.toUpperCase(seatingArea);
    

    should be

    seatEconomyOrFirst = scan.next().charAt(0);
    seatEconomyOrFirst = Character.toUpperCase(seatEconomyOrFirst);
    

    You are ignoring the read character and re-using seatingArea. That method of updating character to upper case appears to be an anti-pattern. You could do it in one line. Like,

    seatEconomyOrFirst = Character.toUpperCase(scan.next().charAt(0));