This week's book giveaway is in the iOS forum.
We're giving away four copies of Classic Computer Science Problems in Swift and have David Kopec on-line!
See this thread for details.
Win a copy of Classic Computer Science Problems in Swift this week in the iOS forum!
  • 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

 
Ranch Hand
Posts: 457
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?

 
Saloon Keeper
Posts: 4101
48
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: 457
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: 4101
48
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: 457
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!