# Array out of bound; arrays handling

Nicol Green

Ranch Hand

Posts: 66

posted 6 years ago

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!

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!

posted 6 years ago

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.

SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6

How To Ask Questions How To Answer Questions

Nicol Green

Ranch Hand

Posts: 66

Bert Wilkinson

Ranch Hand

Posts: 33

posted 6 years ago

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.

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

posted 6 years ago

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!

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

posted 6 years ago

I see what You meant, found some solutions!

Thanks!

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!