0

New to programming and just started Java. I am unable to get the != operand to work with char.This is with regards to the while loop.even though the correct input is being placed. The loop keeps saying, it is an invalid input, despite the correct input being placed.

Input is correctly recognised and is converted to upper case. But the while loop is not working. Any suggestions would be appreciated thanks!

    Scanner keyboard = new Scanner(System.in); //Create object to read user data

    System.out.println("Please enter your age between 1 - 110");
    age = keyboard.nextInt();
    while((age < 1) || (age > 110)){
        System.out.println("Error please input a valid age");
        System.out.println("");
        System.out.println("Please enter your age between 1 - 110");
        age = keyboard.nextInt();
    }

    System.out.println("Please enter this person's gender (M/F)");
    gender = keyboard.next().charAt(0);
    gender = Character.toUpperCase(gender); // Convert all inputted character to upper case

    while((gender != 'M') || (gender != 'F')){
        System.out.println("Error please input a valid gender");
        System.out.println("");
        System.out.println("Please enter this person's gender (M/F)");
        gender = keyboard.next().charAt(0);
        gender = Character.toUpperCase(gender); // Convert all inputted character to upper case 
    }


    System.out.println("Please enter whether this person watches the show regularly (Y/N");
    show = keyboard.next().charAt(0);
    show = Character.toUpperCase(show); // Convert show to upper case


    while((show != 'Y') || (show != 'N')){
        System.out.println("Error - input invalid");
        System.out.println("");
        System.out.println("Please enter whether this person watches the show regularly (Y/N");
        show = keyboard.next().charAt(0);
        show = Character.toUpperCase(show); // Convert show to upper case
    }

    System.out.println("Do you want to enter another person's details (Y/N)?");
    details = keyboard.next().charAt(0);
    details = Character.toUpperCase(details);

    while((details != 'Y') || (details != 'N')){
        System.out.println("Error - Invalid input");
        System.out.println("");
        System.out.println("Do you want to enter another person's details (Y/N)?");
        details = keyboard.next().charAt(0);
        details = Character.toUpperCase(details);
    }
mandok
  • 17
  • 1
  • 6

2 Answers2

0

There is 3 possibility for gender M, F or other. Lets look at them

| Gender | gender != 'M' | gender != 'F'| (gender != 'M') || (gender != 'F') |
| 'M'    | False         | True         | True                               | 
| 'F'    | True          | False        | True                               | 
| Other  | True          | True         | True                               | 

It mean for any gender ansver will be always True

talex
  • 17,973
  • 3
  • 29
  • 66
0

The condition (gender != 'M') || (gender != 'F') can never be false. If gender is 'M', then gender != 'F' will be true and the OR condition is satisfied. If gender is 'F', then gender != 'M' will be true and the OR condition is satisfied.

You want && (AND), not || (OR):

while (gender != 'M' && gender != 'F') {
    // ...
}
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875