4

I am coding a simple program using a scanner and now want to make sure the user only types "yes" or "no" by comparing the input to these words. I'm using a normal if-statement with || (pipes):

if (!input.equals("yes") || !input.equals("no")) {
    //do something
}

Java (I am using BlueJ) complains about my pipes, it says: "illegal character: '\u00a0'"

What I tried

  • I copied & pasted pipes from the wikipedia site to make sure I am really using correct pipe-characters
  • restarted BlueJ
  • tried using equal signs (&&), same error
  • if I removed the second condition and the pipes it works

Does anyone have an idea why it complains and how I can fix it? – Thanks in advance

LinusGeffarth
  • 27,197
  • 29
  • 120
  • 174
  • u00a0 is a special type of space character (http://www.fileformat.info/info/unicode/char/00a0/index.htm). Perhaps your spaces are not normal. Why don't you try replacing them with normal spaces (using the space bar key)? –  Apr 07 '15 at 16:24
  • 1
    Seems to be the same problem as in [Illegal character with Blue J](http://stackoverflow.com/questions/17626909/illegal-character-with-blue-j). – rgettman Apr 07 '15 at 16:24
  • For the record, typing in "no" as input will enter this if block because of short circuiting of your code. When you put in "no", then the first condition of the input not being "yes" is true, and it never evaluates the second condition. – Aify Apr 07 '15 at 16:36
  • @Aify it will enter it as the expression is true for any value of input. For it to evaluate to false, input has to be equal to both "yes" and "no". – drRobertz Apr 07 '15 at 16:41
  • I only pointed out "no" as an example. You are right, the short circuit condition always applies. – Aify Apr 07 '15 at 16:41
  • Yes, and I merely pointed out that it does not depend on short-circuit evaluation, the expression is a logical tautology. So we agree. – drRobertz Apr 07 '15 at 16:47

2 Answers2

5

The character u00a0 is a non-breaking space.

It seems like you typed a control char in your code, replace everything from a copy of this line

if(!input.equals("yes") && !input.equals("no"))

Or simply suppress the spaces in your line and re-type them (making sure you don't type in nbsp).

Notice that I changed the || to && as typing "no" for example would still enter the condition as "yes" would not equals to input. Your condition would then be true || false which would result in true.

In any case, this is not related to the || or && operator, it is the spaces.

Jean-François Savard
  • 20,626
  • 7
  • 49
  • 76
  • So, I used the `!` on purpose, I want to give an error message if the word is **not** one of these – LinusGeffarth Apr 07 '15 at 16:26
  • @LinusG. Alright, I edited to put the negation back. – Jean-François Savard Apr 07 '15 at 16:27
  • Wow, it does work now, the weird thing is that now my version works as well...what did you actually change? – LinusGeffarth Apr 07 '15 at 16:28
  • @LinusG I simply typed the line by myself. The spaces in your line were non-breaking spaces instead of normal spaces which is not a valid character for the compiler. This is not visible by eyes, but *"illegal character: '\u00a0'"* tell us it is incorrect. A normal space is `U0020` – Jean-François Savard Apr 07 '15 at 16:29
  • I don't quite get the meaning of a non-breaking space. Can you explain that? – LinusGeffarth Apr 07 '15 at 16:34
  • @Linus G.:Think about if your condition (x is not "yes" or x is not "no") can evaluate to false. Look up de Morgan's law. – drRobertz Apr 07 '15 at 16:34
  • @LinusG. nbsp is a special character http://en.wikipedia.org/wiki/Non-breaking_space – Aify Apr 07 '15 at 16:34
  • @LinusG. non-breaking spaces are mostly used in HTML. It forces text to be displayed on the same line. If a non-breaking space is found between two words, the line won't break, while it would if it encounter a normal space. – Jean-François Savard Apr 07 '15 at 16:35
  • @LinusG. There exists some other non-breaking character such as non-breaking hyphen for example, but to eyes they are all the same. – Jean-François Savard Apr 07 '15 at 16:36
  • 1
    Thanks you all for the mass of comments and non-frustration while explaining. @Jean-FrançoisSavard: still, my way was completely fine (I think). I want that if the user does not type either one, so anything else than "yes" or "no" it will go through the if-block. – LinusGeffarth Apr 07 '15 at 17:06
  • @LinusG. You're welcome. But what will happen if the user type "no" ? It will enter the condition because it is not equals to "yes". However it should not enter as "no" is not "anything else than 'yes' or 'no". – Jean-François Savard Apr 07 '15 at 17:07
  • The thing is I want the if-block to run whenever the user types an illegal answer, something else than "yes" or "no". – LinusGeffarth Apr 07 '15 at 17:10
  • @LinusG. I know that. This is why you need to use `&&` instead of `||` because you will entry the if-block if you type "no" when using `||` and also if you type "yes" because the other part of the validation will be true and it is an OR. – Jean-François Savard Apr 07 '15 at 17:12
2

'\u00a0'parses to a non breaking space, so maybe it's not the pipes that are the problem. try eliminating the space around the pipes and enclosing the negated statements in parentheses and see if the error persists.

bDombrosky
  • 21
  • 1