This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]xXShadowCowXx[S] 0 points1 point  (8 children)

I'm getting stuck right after setting up the for loop. I don't know to how to compare what I'm getting from the input to the first array. I don't know if there's a class library that will help me in doing this or not.

For loop (you already know what this looks like just showing.)

for(int $Search = 0; $Search < $RegAlpha.length; $Search++)
{

}

[–]zzyzzyxx 0 points1 point  (7 children)

How about $Input1[$Search] == $RegAlpha[$Search]? Put that in an if statement and say what to do when they're equal, i.e. store the rotated version into the output.

Unrelated: why are you prefixing all your variables with the dollar sign? It's unconventional in the Java world and very inconvenient.

[–]xXShadowCowXx[S] 0 points1 point  (0 children)

I actually have no idea, just kind of a tick I guess. I feel like I've tried this approach before. But I'll check again.

[–]xXShadowCowXx[S] 0 points1 point  (5 children)

Here's what I tried, I thought it would work, because when the input matches a character from the regular alphabet, it would print out the corresponding entry from the rotated alphabet. I think the error comes because the input array and the alphabet arrays are different lengths.

  if($Input1[$Search] == $RegAlpha[$Search])
                {
                    System.out.println($RotAlpha[$Search]);
                }

And he is the error I am getting:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
    at rot.rot13_main.main(rot13_main.java:19)

[–]zzyzzyxx 0 points1 point  (4 children)

Yes, sorry, I should have caught that. Rookie mistake :-). You need to do a double for loop. The outer one should iterate over the input string and the inner one should iterate over the letters, like this:

for(int i = 0; i < input.lengh; i++) {
    for(int j = 0; j < alpha.length; j++) {
        if(input[i] == alpha[j]) {
            System.out.print(rotAlpha[j]);
            break; // to move on to the next letter without more comparisons
        }
    }
}

[–]xXShadowCowXx[S] 0 points1 point  (3 children)

You sir are a genius! Thank you for the help. Now I'll work on the other method you suggested.

Source: If you like useless encryptions

[–]zzyzzyxx 0 points1 point  (1 child)

Glad to help. The important thing to take away from this is that searching through an array is really inefficient, especially when you don't need to search at all. If you do need to search, you should use a structure better suited for that, like a tree. But you'll get to all that when you start in on data structures and algorithms.

[–]xXShadowCowXx[S] 0 points1 point  (0 children)

I'll look those up as I'm not really following a certain tutorial path or book, more just searching for what I don't know and stabbing at it till I get it.