• 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

Array out of bound; arrays handling

 
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have two words, which I suppose to encrypt. The encryption is Vigenere: do addition of two chars,
and if addition result bigger than 26 do mod operation, and you should get encrypted char.
The problem is when I have shorter keyword.
Let's suppose that words are:
Message - counts 7 chars
keys - counts 4 chars
I want to say when you come to s, start from beginning for keyword,
BUT continue to the next char in message word. I tried to do something like this:




The error is: Array out of bounds: 4. I suppose this happends because first for is in while.
I don't know how to refer to the next char array for message,
and to add keyword chars from beginning.
Please help me with this.

Thank You!

 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
On line 10 you loop from 0 to msg.length() (exclusive). You then get both msg.charAt(i) and k.charAt(i) for those indexes on line 12. However, on line 8 you have determined that k.length() < msg.length(). That means that you will, 100% guaranteed, run into a value i that is >= k.length(), and k.charAt(i) does not exist. That will trigger an IndexOutOfBoundsException.
 
Nicol Green
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok I see where is the problem.

Thank You!

Do You have any suggestions how to deal with this problem ?
 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I didn't dive into your code, but it appears you have a conceptual .....hmmmm..... "hickup" on modular arithmetic.

you want to index into something of a given length, but you may "lap through it" many times with a long word you are going to encrypt. google MODULAR ARITHMETIC and you'll probably see plenty of examples. All modular arithmetic does is give you the remainder of the division.

for instance:

10 % 10 = 0
100 % 10 = 0
3 % 10 = 3
13 % 10 = 3

Starting to see something useful?

If you want to toy around with this, you can try as many examples as you like with Excel, using the MOD(x,y) function.
 
Nicol Green
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks! I already have implemented method with mod operation, it is

encryptCharVigenere(char c, char k);

But still, main problem has not resolved.
I don't know if I have good logic, or is there better way to look on this problem and to solve it ?

Please, if someone can help me, any ideas will be appreciated!
Thank You in advance!



 
Nicol Green
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

you want to index into something of a given length, but you may "lap through it" many times with a long word you are going to encrypt. google MODULAR ARITHMETIC and you'll probably see plenty of examples. All modular arithmetic does is give you the remainder of the division.



I see what You meant, found some solutions!

Thanks!
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic