Java If statement to check a user input value ensuring it's less than a set int variable

int totalCount = total1 + total2 + total3 + total4;
int MaxTotal = 32;
int MaxTotalCal = MaxTotal - (totalCount);


System.out.print("\nEnter First name: ");
Scanner FN = new Scanner(System.in);
String fn = FN.nextLine();

System.out.print("Enter Last name: ");
Scanner LN = new Scanner(System.in);
String ln = LN.nextLine();

System.out.print("Enter Phone number: ");
Scanner Num = new Scanner(System.in);
String num = Num.nextLine();


System.out.print("Adults in party: ");
Scanner adult = new Scanner(System.in);
int Adult = adult.nextInt();

System.out.print("Kids in party: ");
Scanner kid = new Scanner(System.in);
int Kid = kid.nextInt();

int inputTotal = MaxTotalCal - Kid - Adult;

if (inputTotal > MaxTotal) {
    System.out.print("Error");
} else{
         System.out.print("\n"+(uniqueID + " ||"));      
         System.out.print(fn+ " ||");
         System.out.print(ln+ " ||");
         System.out.print(num+ " ||");
         System.out.print(Adult+ " ||");
         System.out.print(Kid+ " ||");
         System.out.println("\nTotal seats left to book: " + inputTotal);
}

System.in.read();

Essentially I'm trying to add the user input for adults and kids to the total, if the total is more then 32 then the class will restart, otherwise if it's less than 32 then execute the print statements. Where have I gone wrong?

Thank you beforehand for any help given, I'm rather new to Java

1 answer

  • answered 2018-01-13 18:06 azro

    You should change some things:

    • name your variable in camelCase
    • use only one Scanner, don't need different ones
    • use a while loop to ask again until the condition is valid : sum<maxTotal
    • give more explicit name to your variable, maxTotalCal is not very good

    Here is code that works, and ask until kid+adult is not under 32

    int totalCount = total1 + total2 + total3 + total4;
    int maxTotal = 32;
    int maxTotalCal = maxTotal - (totalCount);
    Scanner scanner = new Scanner(System.in);
    
    System.out.print("\nEnter First name: ");  String fn = scanner.nextLine();
    
    System.out.print("Enter Last name: ");  String ln = scanner.nextLine();
    
    System.out.print("Enter Phone number: ");  String num = scanner.nextLine();
    
    System.out.print("Adults in party: ");
    int adult = Integer.parseInt(scanner.nextLine());
    
    System.out.print("Kids in party: ");
    int kid = Integer.parseInt(scanner.nextLine());
    
    int inputTotal = maxTotalCal - kid - adult;
    
    while ((kid + adult) > maxTotal) {
        System.out.println("Please enter valid numbers, sum should be under " + maxTotal);
        System.out.print("Adults in party: ");
        adult = Integer.parseInt(scanner.nextLine());
        System.out.print("Kids in party: ");
        kid = Integer.parseInt(scanner.nextLine());
    }
    
    System.out.print("\n" + (uniqueID + " ||"));
    System.out.print(fn + " ||");
    System.out.print(ln + " ||");
    System.out.print(num + " ||");
    System.out.print(adult + " ||");
    System.out.print(kid + " ||");
    System.out.println("\nTotal seats left to book: " + inputTotal);