• 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
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

Cipher and Decipher: Out of Bounds Exception

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Okay I am currently stuck and have not been able to get past this assignment in my Computer Science Class. My program has to create a randomly generated alphabet then cipher a message. Both the alphabet and message are then printed to a text file. Part 2 of it has reading the random alphabet and decoding the message. My encryption class works fine but the decryption class is the killer. If you could help it would be greatly appreciated. The project folder and assignment instructions are attached.





 
Bartender
Posts: 1558
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Christopher,

Welcome to CodeRanch!

I could not find the attached project folder and assignment instructions.

Besides, at CodeRanch, we strongly encourage users to learn on their own, instead of providing ready-made solutions. Needless to say, you can ask/discuss any doubt you are facing during your assignments and we'll help you in finding the solution (instead of giving you the solution )

Also, please be specific about the problem. You've only provided a small mention of problem (in subject line) - you are facing out of bounds exception. Can you please be more specific about it (e.g. providing call stack etc)?

From initial look, I can see that in your ChrisGoadDecipher class, you are looping over cipherAlphabet for 26 times. What if cipherAlphabet's size is less than 26?

I hope this will help.
 
Christopher Goad
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm sorry I implied I wanted the answers. That is not what I meant.
Anyway, I know the problem is in my decryption class. The error always occurs when reading back the cipher alphabet from the txt file. Could you compile my code and take a look for yourself?
 
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

Christopher Goad wrote:Anyway, I know the problem is in my decryption class. The error always occurs when reading back the cipher alphabet from the txt file.


Actually, I suspect that's not the case. The problem looks to be in your createNewAlphabet() method, which I'm still not quite sure what it's doing, but it doesn't appear to use a Caesar shift.

Assuming that you simply want to use a random combo of the original letters to encrypt a message (and if you want your encryption to allow blanks, it should probably have 27, not 26 - and 53 if you want to allow mixed case), consider using Collections.shuffle(); and if you want your decrypt() function to work nice and fast, I'd suggest that you put the result of that shuffle into a HashMap (specifically, a HashMap<Character, Integer>).

If you truly want to use a Caesar shift, then your new "alphabet" MUST be a rotation of the original - so have a look at the 'mod' (%) operator.

HIH

Winston
 
Christopher Goad
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's what I meant. Anyway in my createNewAlphabet() method i am trying to read to read the random alpabet i made in the txt file and then assign it to the cipherAlphabet array.
 
Winston Gutkowski
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

Christopher Goad wrote:That's what I meant.


Erm...what's what you meant?

Anyway in my createNewAlphabet() method i am trying to read to read the random alpabet i made in the txt file and then assign it to the cipherAlphabet array.


So, do you:
(a) Know whether you read it back in correctly.
(b) Know whether your method is translating it back correctly?
And the easiest way to track that is with debug statements (or, indeed, a debugger)...

The fact is that, to decrypt, the ONLY thing you should need is your jumbled "alphabet".

Winston
 
Christopher Goad
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I meant the problem was in the ChrisGoadDecipher class but more specificly in the createNewAlphabet() method. I do not. Could you help me figure the problem out because I am not sure what is going on.
 
Christopher Goad
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I figured out the problem in my createnewalphabet() method. I didnt have it changed to a char.

Now I have one last problem. I need to reverse the cipher in my decrypt method. How could this be done?

 
Christopher Goad
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ahah, I got it. All I had to do was flip the cipherAlphabet and Alphabet!

It works perfect now!



One last problem.

Whenever I enter yes to tryAgain it does nothing


 
Winston Gutkowski
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

Christopher Goad wrote:One last problem.
Whenever I enter yes to tryAgain it does nothing


You know what? Compared to what you've just solved that's a fart in a storm; so I'm going to leave it up to you.

Two tips (and I say this without even having looked at your code):
1. AvoidTheEqualityOperator (←click).
2. Add debug code, and TEST.

There are some things you just can't get around.

Winston
 
Christopher Goad
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Okay, I understand. Can't believe I got that by myself(With some small hints). Thanks for the help.

I'll be back lol
 
Winston Gutkowski
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

Christopher Goad wrote:Okay, I understand. Can't believe I got that by myself(With some small hints). Thanks for the help.


Glad to help - and isn't it much better when you DO get it yourself?

Winston
 
reply
    Bookmark Topic Watch Topic
  • New Topic