zactrixo
zactrixo

Reputation: 71

Loop always executing twice

I'm currently doing a school project and in that project I have to ask the user his/her gender and then display an error which works. But for some reason the loop always executes twice before letting the user input again. example:

Veuillez entrer le sex de l'individu f/F ou h/H: e

Erreur veuillez entrer des valeurs valides
Veuillez entrer le sex de l'individu f/F ou h/H: 

Erreur veuillez entrer des valeurs valides
Veuillez entrer le sex de l'individu f/F ou h/H: 

and here is the code

 System.out.println ("Veuillez entrer le sex de l'individu f/F ou h/H: "); //first time asking for user input
 sex = Clavier.lireChar();

 while ((sex != ('f') || sex != ('F')) || (sex != ('h') || sex != ('H'))){ 
       System.out.println ("Erreur veuillez entrer des valeurs valides");  //error message
       System.out.println ("Veuillez entrer le sex de l'individu f/F ou h/H: "); //asking for user inoput again
       sex = Clavier.lireChar();
 }
 if (sex == ('f') || sex == ('F')) {
       nbFemmes = nbFemmes + 1;
 } else if (sex == ('h') || sex == ('H')) {
       nbHommes = nbHommes +1;
 }                        

Upvotes: 0

Views: 203

Answers (3)

that other guy
that other guy

Reputation: 123460

The problem is that when you press f followed by Enter, you have pressed two keys and therefore get two chars, one lowercase letter f ('f') and one linefeed/newline character ('\n').

You can similarly enter abc and press enter, and you'll see it loops 4 times instead of 1.

You can fix this in one of two ways. The easiest is probably to read a char by reading an entire line and then only using the first letter. I don't know what this Clavier class is, but if it's similar to this one you can use something like:

sex = Clavier.lireChaine().charAt(0);

Alternatively, you can keep reading single characters until you get something that is not a linefeed:

while ((sex = Clavier.lireChar()) == '\n');

It will now no longer loop twice for every attempt, but due to the problem mentioned by Tushar, it'll keep asking forever and never accept any input because if you enter f then sex != 'h' is true, and if you enter h then sex != 'f' is true, and either condition causes the loop to repeat.

Upvotes: 1

foreza
foreza

Reputation: 46

Probably late to the party, here's another solution.

As a couple of the people mentioned, the issue looks to be with your condition inside the while loop.

I'm a bit concerned that your teacher doesn't allow use of scanner, though!

Scanner in = new Scanner(System.in);
System.out.println ("Veuillez entrer le sex de l'individu f/F ou h/H: ");

char sex = in.next().charAt(0); 

while (sex != 'f' && sex != 'F' && sex != 'h' && sex != 'H' ){ 
System.out.println ("Erreur veuillez entrer des valeurs valides");  //error message
System.out.println ("Veuillez entrer le sex de l'individu f/F ou h/H: "); //asking for user input again
sex = in.next().charAt(0); 
}

// If we reached here, this means we have good input.

if (sex == ('f') || sex == ('F')) {
 // Do something
} else if (sex == ('h') || sex == ('H')) {
 // Do something
}    

Upvotes: 0

Md Golam Rahman Tushar
Md Golam Rahman Tushar

Reputation: 2375

The problem is with your while condition logic. Try the following and make sure you understand it. Let me know if you don't understand the conditional logic. Happy coding!

while ((sex != ('f') && sex != ('F')) && (sex != ('h') && sex != ('H'))){ 
   System.out.println ("Erreur veuillez entrer des valeurs valides");  //error message
   System.out.println ("Veuillez entrer le sex de l'individu f/F ou h/H: "); //asking for user inoput again
   sex = Clavier.lireChar();
}

Upvotes: 2

Related Questions