aspose file tools*
The moose likes Beginning Java and the fly likes Array out of bound; arrays handling Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Array out of bound; arrays handling" Watch "Array out of bound; arrays handling" New topic
Author

Array out of bound; arrays handling

Nicol Green
Ranch Hand

Joined: Jan 30, 2010
Posts: 66
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!

Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19790
    
  20

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
How To Ask Questions How To Answer Questions
Nicol Green
Ranch Hand

Joined: Jan 30, 2010
Posts: 66
Ok I see where is the problem.

Thank You!

Do You have any suggestions how to deal with this problem ?
Bert Wilkinson
Ranch Hand

Joined: Oct 28, 2009
Posts: 33
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
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
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!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Array out of bound; arrays handling