• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Rearranging an array with offsets

 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 250
1
Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic