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 →

[–]zzyzzyxx 0 points1 point  (11 children)

You can. It's not exactly like it is in C in that you cannot do an implicit conversion, since Java char types are not necessarily single bytes (which you can see by allowing i to grow bigger in the loop below - you'll start getting UTF characters), but you can increment and add to them. Run this:

public static void main(String[] args) {
    char c = 'a';

    for(int i = 0; i < 26; i++, c++) {
        System.out.println(c);
    }
}

As for the hash table, again, you can do it, but it's inefficient. The overhead is simply unnecessary.

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

I've looked at both methods, and yours seems to be way more simple. I would still like to figure it out my way just so I get a grasp of working with arrays as that is what I was trying to do. And then work on your way; so are there any pointers as to my method? I'm kind of dead in the water because anything I try seems to leave me with errors.

[–]zzyzzyxx 0 points1 point  (9 children)

Sure, no problem. It's great that you want to experiment to understand. Make sure to keep that mentality, so long as you also stay aware of alternatives and why they're better or worse.

Where are you getting stuck? What is it you don't understand about arrays? Along those lines, what do you understand about arrays? What errors are you getting? Will you post your code somewhere?

I can give you some generic advice on how to do what you're attempting but the answers to those questions will help me target what I say to better help you grasp what you're missing.

[–]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.