Reputation: 13
I know there are bunch of topics about Caesar Cipher but I like to solve things my way. And as such it doesnt work ,but I think with some help I might get it to work my way.I am sure you all know that good feeling when you alone solve the problem.
So here is my idea. To make array of chars that consist alphabet. And String with message to code. 2 for loops. one outer to set char from message, and inner that scans thru alphabet array. When letter from message meet char in array, it replaces him by 3rd (key of 3) char down in array.
Here is piece of code written by now:
char[] alphabet = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'o', 'p','r','s','t','u', 'v', 'z'};
String message = " This is message for coding";
message = message.toLowerCase();
String codedMsg = "";
for(int i = 0; i < message.length(); i++)
{
for(int j =0; j < alphabet.length; j++)
{
if(message.charAt(i) == alphabet[j])
{
codedMsg += alphabet[j +3 ];
It complies well, but I receive following error when run :
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 23
at sifra.main(sifra.java:19)
Upvotes: 0
Views: 237
Reputation: 63
You're problem is that you have alphabet[j + 3]
.
But because j < alphabet.length
, so when j = alphabet.length - 2
say alphabet[j + 3]
becomes alphabet[alphabet.length + 1]
so you go outside the array.
To solve this you can use alphabet[(j + 3)%alphabet.length]
.
Now your code will run but not be correct.
Because you always manipulate message
it will be replaced many times in the inner loop.
for(int j =0; j < alphabet.length; j++)
{
if(message.charAt(i) == alphabet[j])
{
message = message.replace(message.charAt(i), alphabet[j + 3]); // THIS LINE IS THE PROBLEM
If we say that message.charAt(i) = a
this will be true if(message.charAt(i) == alphabet[j])
and all a
in the sting will change to d
so now message.charAt(i) = d
and after 3 iteration of for(int j =0; j < alphabet.length; j++)
the if statement will be true again and all d
in the string will be replaced with g
and so on. My solution to the problem is the following but there are probably many more:
char[] alphabet = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'o', 'p','r','s','t','u', 'v', 'z'};
String message = " This is message for coding";
message = message.toLowerCase();
char[] messageArray = message.toCharArray();
for(int i = 0; i < message.length(); i++)
{
for(int j =0; j < alphabet.length; j++)
{
if(message.charAt(i) == alphabet[j]){
messageArray[i] = alphabet[(j + 3)%alphabet.length];
}
}
}
System.out.println(String.copyValueOf(messageArray));
Upvotes: 2
Reputation: 4125
as others have pointed out, your problem is that alphabet[j+3]
will be out of bounds towards the end of the alphabet. Here's a fix that gets around that.
Replace this
message = message.replace(message.charAt(i), alphabet[j + 3]);
With this
if(j < alphabet.length - 3){
message = message.replace(message.charAt(i), alphabet[j + 3]);
}else{
message = message.replace(message.charAt(i), alphabet[j + 3 - alphabet.length]);
}
This should loop the alphabet back to the beginning if it ends up near the end. For instance if the character z
is input, it outputs c
Upvotes: 0
Reputation: 40203
The problem, as mentioned in comments, is that alphabet[j + 3]
goes out of bounds for j
greater or equal to alphabet.length - 3
. The solution I can suggest is changing alphabet[j + 3]
to alphabet[(j + 3) % alphabet.length]
, which will have a "cyclic" behaviour once the index goes out of bounds.
Upvotes: 1
Reputation: 4554
alphabet[j + 3]
is going out of the size of the array alphabet
.
Upvotes: 2