0

I am trying to design an airline reservation system. The code should be self-explanatory. The generateRandom methods output a random number. However, after the first while-loop executes, the program stops running, which means the loop is infinite and the compiler never reaches the next line of code. Why?

import java.util.*; public class Main {

public static void main(String[] args) {
    
    
    //initialization of passenger LL
    LinkedList<Passenger> PassLL = new LinkedList<Passenger>();       
 
    Scanner input2 = new Scanner(System.in);
    System.out.println("Please enter your preferred class.");
    String clasz = input2.nextLine();
    
    while(true) {
        clasz = input2.nextLine();
        if(clasz == "Economy" && generateRandom.erand < 30) {       
            break;}
        if(clasz == "Business" && generateRandom.erand < 18) {      
            break;}
        if(clasz == "First" && generateRandom.erand < 8) {      
            break;}
        if(clasz == "Economy" && generateRandom.erand == 30) {
            System.out.println("I'm sorry, economy is full. Please choose another class.");}
        if(clasz == "Business" && generateRandom.brand < 18) {
            System.out.println("I'm sorry, business is full. Please choose another class.");}
        if(clasz == "First" && generateRandom.frand < 8) {
            System.out.println("I'm sorry, first is full. Please choose another class.");}
        }
    
    
    Scanner input6 = new Scanner(System.in);
    System.out.println("How many tickets would you like to book?");
    int tickets = input6.nextInt();



  

  

}

}

  • 1
    "stops running" and "infinite loop" sounds like opposites – Ivo Mar 08 '22 at 08:38
  • Using `==` is not how you compare strings for equality. Use the String#equals() method instead, for example: `clasz.equals("Economy")`. Also condider using `else if`. – DevilsHnd - 退職した Mar 08 '22 at 12:43
  • Does `generateRandom.erand` and `generateRandom.frand` generate a new random number when called upon? If so then it would be better to call them once at the start of your while loop: `while (true) { int ernd = generateRandom.erand; int frnd = generateRandom.frand; if (clasz.equals("Economy") && ernd < 30) {` and so on. – DevilsHnd - 退職した Mar 08 '22 at 12:56

0 Answers0