• Post Reply Bookmark Topic Watch Topic
  • New Topic

Rearranging an array with offsets  RSS feed

 
Josh Galeigh
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm trying to take an input string of the alphabet and create an offset on it so that it starts with say 'D' and ends with XYZABC. Simply starting from an offset from A and finishing with whatever the offset skipped. I have no problem skipping the first few letters but it is tagging on what was skipped that I can't seem to get.
Here's my code:


I ask for the offset and the string (The user can input the alphabet already offset). I put the string into an array and work on it from there. Here is where I'm stuck:



It seems to me that I should be able to subtract my offset from the end of the array then start from A (ASCII 65) and add up until I've hit the end of the array. However, right now if I put the correct (non-offset) alphabet in and create offset of 3 instead of the alphabet ending with WXYZABC. It ends with WXYZD. I don't understand why my code doesnt work. I can make it say WXYZA by putting in manual offsets in the for loop but it wont print out anything beyond the first character.

Thanks for the help.
 
Joel Christophel
Ranch Hand
Posts: 250
1
Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're definitely over-complicating things. All you need is a loop that starts at the offset and iterates through ABC and then a loop that starts at 0 and stops at the offset. You don't even really need your second cipher array to store if you're just outputting the information.

You also don't need to do anything with ASCII values. Just use the indices of the array.
 
Josh Galeigh
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joel Christophel wrote:You're definitely over-complicating things. All you need is a loop that starts at the offset and iterates through ABC and then a loop that starts at 0 and stops at the offset. You don't even really need your second cipher array to store if you're just outputting the information.

You also don't need to do anything with ASCII values. Just use the indices of the array.


Yeah, I guess I was going a bit too far with that... That seems to be my first step for anything I do in Java.

Thanks
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Josh Galeigh wrote:Yeah, I guess I was going a bit too far with that... That seems to be my first step for anything I do in Java.

Don't worry; you're not alone. That's why it's always a good idea to write things down before you code.

Tip: You can also do this using remainders. Have a think how that might work.

Winston
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!