• Post Reply Bookmark Topic Watch Topic
  • New Topic

String "A" not recognized as character although debugger say that the ascii code is 65  RSS feed

 
Giovanni Montano
Ranch Hand
Posts: 434
7
Android Open BSD Slackware
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was running this method and the row firstLetter="A" is not recognized as a true condition, so the loop springs to the else condition. Why?

 
Carey Brown
Saloon Keeper
Posts: 3329
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Using == to compare strings does not work the way you might expect. It will compare String references, which is not want you want. Use "A".equals(b) instead. This compares the actual Strings that are being referred to. Note that you could also use b.equals("A") but that will give you a null pointer exception in the event that b is null.
 
Giovanni Montano
Ranch Hand
Posts: 434
7
Android Open BSD Slackware
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:Using == to compare strings does not work the way you might expect. It will compare String references, which is not want you want. Use "A".equals(b) instead. This compares the actual Strings that are being referred to. Note that you could also use b.equals("A") but that will give you a null pointer exception in the event that b is null.


I see, thank you very much for your help.
I have also a question regarding patterns, can I publish a second OP post or is not the case, and should I post here or in the design patterns forum? Is really a general one about how to find an algorithm without too many if, that is testable.
 
Carey Brown
Saloon Keeper
Posts: 3329
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Giovanni Montano wrote:I see, thank you very much for your help.
I have also a question regarding patterns, can I publish a second OP post or is not the case, and should I post here or in the design patterns forum? Is really a general one about how to find an algorithm without too many if, that is testable.

You're welcome.

If your next question pertains to this code then continue to add to this thread.
Else If your question is "beginner" level, then create a new thread in the beginner forum.
Else post your question in the design patterns forum.
 
Giovanni Montano
Ranch Hand
Posts: 434
7
Android Open BSD Slackware
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:
Giovanni Montano wrote:I see, thank you very much for your help.
I have also a question regarding patterns, can I publish a second OP post or is not the case, and should I post here or in the design patterns forum? Is really a general one about how to find an algorithm without too many if, that is testable.

You're welcome.

If your next question pertains to this code then continue to add to this thread.
Else If your question is "beginner" level, then create a new thread in the beginner forum.
Else post your question in the design patterns forum.


second condition, beginner design pattern, wannabe super Mocker
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!