Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Input a single char.Comparisson failed

  1. #1
    Member
    Join Date
    Jul 2012
    Posts
    90
    Thanks
    1
    Thanked 2 Times in 2 Posts

    Default 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!
    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).


  2. #2
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: Input a single char.Comparisson failed

    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)

    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.

  3. #3
    Member
    Join Date
    Jul 2012
    Posts
    90
    Thanks
    1
    Thanked 2 Times in 2 Posts

    Default 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?

  4. #4
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: Input a single char.Comparisson failed

    Quote Originally Posted by Samaras View Post
    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...

    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:

    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.
    Last edited by pbrockway2; August 25th, 2012 at 10:37 PM.

  5. #5
    Member
    Join Date
    Jul 2012
    Posts
    90
    Thanks
    1
    Thanked 2 Times in 2 Posts

    Default 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

Similar Threads

  1. preparedStatement.executeBatch failed
    By liron50 in forum JDBC & Databases
    Replies: 4
    Last Post: August 11th, 2012, 02:24 AM
  2. Isolating a single word from user input...
    By ninjaBob in forum What's Wrong With My Code?
    Replies: 3
    Last Post: January 15th, 2012, 04:29 PM
  3. How to check if a user input is a char or a int
    By Blackbird94 in forum Java Theory & Questions
    Replies: 3
    Last Post: October 8th, 2011, 04:17 PM
  4. Replies: 3
    Last Post: April 11th, 2011, 09:51 PM
  5. [SOLVED] Char type Comparisson
    By solo_2101 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: April 26th, 2010, 09:42 PM