Win a copy of Microservices in Action this week in the Web Services forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

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

 
Ranch Hand
Posts: 484
10
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: 5168
54
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: 484
10
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: 5168
54
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: 484
10
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:)
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!