• Post Reply Bookmark Topic Watch Topic
  • New Topic

find the index of the first letter of the string  RSS feed

 
William Pen
Greenhorn
Posts: 7
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


String loc is a location on a chessboard (ex: e4, d8, b3). The goal is to find the index of the first letter of the string and set col equal to that index. The issue is that the code shown above returns 0 for all letters. Please help.
 
Norm Radder
Rancher
Posts: 2240
28
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use the equals() method to compare String objects.

The definition of ah should be outside of the loop if its contents is  not changing inside of the loop.

The loop's iteration count should use the length value of ah to determine how many elements in ah to look at.  There should NOT be a hardcoded 7 to control the loop
 
Carey Brown
Saloon Keeper
Posts: 3310
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alternative without substring().

*OR*
 
Carey Brown
Saloon Keeper
Posts: 3310
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
William Pen wrote:

String loc is a location on a chessboard (ex: e4, d8, b3). The goal is to find the index of the first letter of the string and set col equal to that index. The issue is that the code shown above returns 0 for all letters. Please help.

The reason this returns 0 is that you are using == to compare Strings. == only compares the reference, not the actual String contents. For that you should use the equals() method.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!