1

I am trying to convert this buffered reader to a string but for some reasong the if statement will not recognize the first letter as a string

BufferedReader userInputMessage = new BufferedReader(new InputStreamReader(System.in));
message = userInputMessage.readLine();

firstLetter = message.substring(0,1);

System.out.println("FirstLetter: " + firstLetter + "/");
message = message.substring(1);
System.out.println("Message: " + message + "/");

if(!firstLetter.equals("0") || !firstLetter.equals("1")){
    System.out.println("First letter not valid!");
}
9
  • re "Converting this bufferedreader to string" -- A BufferedReader is not a String, nor can it be "converted" into a String. Commented Oct 20, 2014 at 21:06
  • 2
    How did you declare firstLetter? Commented Oct 20, 2014 at 21:06
  • 2
    Also, if your code is causing an error, please print the complete error message, and also indicate which line is causing the error to occur. Commented Oct 20, 2014 at 21:07
  • declared firstLetter as a string Commented Oct 20, 2014 at 21:07
  • Does it equal 0 or 1? Commented Oct 20, 2014 at 21:08

1 Answer 1

2

This will always be true:

if(!firstLetter.equals("0") || !firstLetter.equals("1")){ 

think about it. If first letter is 0, then the second part is true, if it's 1, the first part will be true, if it's anything else, both will be true. You want to use && perhaps:

if(!firstLetter.equals("0") && !firstLetter.equals("1")){ 
Sign up to request clarification or add additional context in comments.

4 Comments

Simple but sometimes difficult to spot. Good catch! + 1
Thanks! it is sometimes difficult to spot
!case1 || !case2 should as pattern immediately ring an alarm.
@JoopEggen: yep. A simple mental Venn diagram will show a big problem if case1 and case2 have an empty union set.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.