Java char comparison does not seem to work -
i know can compare chars in java normal operators, example anysinglechar == y
. however, have problem particular code:
do{ system.out.print("would again? y/n\n"); looper = inputter.getchar(); system.out.print(looper); if(looper != 'y' || looper != 'y' || looper != 'n' || looper != 'n') system.out.print("no valid input. please try again.\n"); }while(looper != 'y' || looper != 'y' || looper != 'n' || looper != 'n');
the problem should not other method, inputter.getchar(), i'll dump anyway:
private static bufferedreader read = new bufferedreader(new inputstreamreader(system.in)); public static char getchar() throws ioexception{ int buf= read.read(); char chr = (char) buf; while(!character.isletter(chr)){ buf= read.read(); chr = (char) buf; } return chr; }
the output i'm getting follows:
would again? y/n n nno valid input. please try again. again? y/n n nno valid input. please try again. again? y/n
as can see, char put in n
. printed out correctly(hence seen twice). however, comparison doesn't seem become true.
i'm sure i'm overlooking obvious.
your logic incorrect. true
looper
isn't 'y'
or isn't 'y'
or isn't ...
you want logical operator "and": &&
if(looper != 'y' && looper != 'y' && looper != 'n' && looper != 'n')
and similar change in while
condition.
Comments
Post a Comment