• Post Reply Bookmark Topic Watch Topic
  • New Topic

Encoder  RSS feed

 
Derek Smiths
Ranch Hand
Posts: 119
Eclipse IDE Mac VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I usually make my posts here as i have better luck at getting a response, so I will attach the link to the other post here


I have an encoder that will shift the character of a string X places. Im trying to account for the possibility of the shift being larger than alphabet array length. I've come up with the following, but I still get an ArrayIndexOutOfBounds Error and it never gets past the if statement.

 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37469
539
Eclipse IDE Java VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Derek,
What are you hoping for this statement to do?


I don't think it does what you expect. str is of length one so it would never be greater than 26.

If you aren't attached to your implementation, there is a far simpler way to accomplish what you want. A character is a numeric type. (Weird isn't it.) That means you can add the number of positions to it that you want to shift. For example, 'B' + 3 is 'D'.

That just leaves you with the logic for when you have gone past 'Z' or 'z'. Which you can use an if statement and subtract 26 to get back.
 
Derek Smiths
Ranch Hand
Posts: 119
Eclipse IDE Mac VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Jeanne,

I was using str.indexOf to find the location of that letter in the alphabet array (which should have ignorcase( ) ), and would add to that shift value. So, for example, if str was 'y' and the index of that would be 25, and adding the shift value, of say 4, would mean that 29 > 26. Given that, I tried to find a way to find a newIndex and assign that value to str.

And I see now that my newIndex value was not figure correctly -- working on too many programs at once.

Thank you -- delirious and I wandered way off the beaten trail!
 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You may find the remainder operator % quicker to use.
 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is that encoder the same as a Caesar cipher?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!