Input a single char.Comparisson failed
I input the character.The code goes into an infinite loop even though it is supposed(by me i admit) not to!
Code java:
static char getInput() {
boolean error = false;
char c='n';
do {
try {
error = false;
Scanner in = new Scanner(System.in);
String s;
do{
s = in.nextLine();
c=s.charAt(0);
}while( c!='w' ||c!='W'||c!='a'||c!='A'||c!='a'||c!='s'||c!='S'||c!='d'||
c!='D');
} catch (InputMismatchException e) {
System.err.println("Unexpected IO ERROR: " + e.toString());
error = true;
}
} while (error);
return c;
}
The problem is that the comparison fails,because even though i typed w the loop starts again.
What i wanted to do is to get input from the user by the little arrows that are placed below the big enter of the keyboard,but i do not know how to do this(the only think that comes to mind is event on GUI,but i do not want to include GUI now).
Re: Input a single char.Comparisson failed
Quote:
The problem is that the comparison fails,because even though i typed w the loop starts again.
Correct. So think about the condition you are using (because, clearly, it does not mean what you intend it to mean)
Code :
while( c!='w' ||c!='W'||c!='a'||c!='A'||c!='a'||c!='s'||c!='S'||c!='d'||c!='D');
It turns out that that the condition is always true. Suppose c is actually 'w'. In that case c!='w' is false, but the next bit - c!='W' - is true. So the whole disjunction (c!='w' OR c!='W' OR ...) is true.
On the contrary suppose c is something other than 'w'. In that case the first term of the disjunction (c!='w') is true, and therefore the whole disjunction is true. In fact faced with something-true OR something-else OR something-else OR... the runtime won't even look at the other parts of the disjunction.
So your condition is always true because some part or other always is.
A better condition would involve AND rather than OR.
Re: Input a single char.Comparisson failed
Oh my god,what i missed there?? :/ Of course AND is what i need!
Could i do the same thing by letting the user hit the arrows i said in the first post instead of w,a,s,d?
Re: Input a single char.Comparisson failed
Quote:
Originally Posted by
Samaras
Oh my god,what i missed there?? :/ Of course AND is what i need!
It's easy to miss because in English we say "not p or q" to mean "not p and not q". Things are plainer when you wave your hand about to indicate parentheses. But you get strange looks...
Quote:
Could i do the same thing by letting the user hit the arrows i said in the first post instead of w,a,s,d?
No. Basically Java doesn't do character (or key) detection from the keyboard/console. It was one of the first "request for enhancements" registered with Sun: that was shortly after the Jurassic and I think we can safely conclude at this stage that it ain't gonna happen.
This is the sort of thing that is easily tested by making a little test program:
Code :
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
// read a line (there is no method to return a character straight away)
String line = in.nextLine();
// and examine it character by character
for(char ch :line.toCharArray()) {
System.out.println(ch);
}
}
}
I had never noticed it before, but Scanner's behaviour when run at the command line is quite interesting. The up/down arrow keys maintain a history between runs of the program, which is an interesting security hole.
You have more options when writing a graphical (Swing or similar) application. Buttons can be provided or you can handle keys (arrows or letters) registering "handlers": methods that do the right thing when the appropriate gesture is made.
Re: Input a single char.Comparisson failed
1-I am Greek so i do not think that it was english fault.
2-Yeah i noticed too,but not something special :/ Also the right and left do nothing.
Thanks anyway