This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.

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.

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.

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

Joined: Jan 30, 2010
Posts: 66

posted

0

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

Joined: Jan 30, 2010
Posts: 66

posted

0

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.