Java Help Rot13 code

Status
Not open for further replies.

DaVinci

Baseband Member
Messages
22
Ok i have to write a Rot13 code. Basicly the code is to run the program, the program ask the user if they want to code or decode a message. The program then takes the users input and converts the letters 13 steps ahead (IE: A becomes N). Decode is the exact same thing (IE: N would become A again). I'm having 2 major problems.

1) It goes threw the order and changes the letters... but for the first 14 letters in the alphabet it is converting them back. Example: converts A to N, then converts the new N back into a A. How can i get around this? I have been told use ACSII but i havent learned about it yet.

2) I put it into a if else statement. But when the program runs i enter 1 to code a message and it ask for the user to input the code but kicks out of the program without letting the user enter anything. Thanks

Code:
import java.util.Scanner;

public class RotterTest {
    public static void main (String[] args) {
        String messageCoded;
        int messageChoice;
        String vert1, vert2, vert3, vert4, vert5, vert6, vert7, vert8, vert9, vert10, vert11, vert12;
        String vert13, vert14, vert15, vert16, vert17, vert18, vert19, vert20, vert21, vert22, vert23;
        String vert24, vert25, vert26, vert27;
        Scanner scan = new Scanner (System.in);    
        System.out.println("Press 1. For code, Press2. for Decode");
        messageChoice = scan.nextInt();

        if(messageChoice == 1) {        
            System.out.println("Please enter your coded message:");
            messageCoded = scan.nextLine();
            vert1 = messageCoded.replace( 'a', 'n');
            vert2 = vert1.replace( 'b', 'o');
            vert3 = vert2.replace( 'c', 'p');
            vert4 = vert3.replace( 'd', 'q');
            vert5 = vert4.replace( 'e', 'r');
            vert6 = vert5.replace('f', 's');
            vert7= vert6.replace('g', 't');
            vert8 = vert7.replace('h','u');
            vert9 = vert8.replace('i','v');
            vert10 = vert9.replace('j', 'w');
            vert11 = vert10.replace('k', 'x');
            vert12 = vert11.replace('l', 'y');
            vert13= vert12.replace('m', 'z');
            vert14= vert13.replace('n', 'a');
            vert15 = vert14.replace('o', 'b');
            vert16 = vert15.replace('p', 'c');
            vert17 = vert16.replace('q', 'd');
            vert18 = vert17.replace('r', 'e');
            vert19 = vert18.replace('s', 'f');
            vert20 = vert19.replace('t', 'g');
            vert21 = vert20.replace('u', 'h');
            vert22 = vert21.replace('v', 'i');
            vert23 = vert22.replace('w', 'j');
            vert24 = vert23.replace('x', 'k');
            vert25 = vert24.replace('y', 'l');
            vert26 = vert25.replace('z', 'm');
            vert27 = vert26.replace(' ', '$');
            System.out.println(vert27); 
        }
        else if(messageChoice == 2) {
            System.out.println("Please enter your coded message:");
            messageCoded = scan.nextLine();
            vert1 = messageCoded.replace( 'a', 'n');
            vert2 = vert1.replace( 'b', 'o');
            vert3 = vert2.replace( 'c', 'p');
            vert4 = vert3.replace( 'd', 'q');
            vert5 = vert4.replace( 'e', 'r');
            vert6 = vert5.replace('f', 's');
            vert7= vert6.replace('g', 't');
            vert8 = vert7.replace('h','u');
            vert9 = vert8.replace('i','v');
            vert10 = vert9.replace('j', 'w');
            vert11 = vert10.replace('k', 'x');
            vert12 = vert11.replace('l', 'y');
            vert13= vert12.replace('m', 'z');
            vert14= vert13.replace('n', 'a');
            vert15 = vert14.replace('o', 'b');
            vert16 = vert15.replace('p', 'c');
            vert17 = vert16.replace('q', 'd');
            vert18 = vert17.replace('r', 'e');
            vert19 = vert18.replace('s', 'f');
            vert20 = vert19.replace('t', 'g');
            vert21 = vert20.replace('u', 'h');
            vert22 = vert21.replace('v', 'i');
            vert23 = vert22.replace('w', 'j');
            vert24 = vert23.replace('x', 'k');
            vert25 = vert24.replace('y', 'l');
            vert26 = vert25.replace('z', 'm');
            vert27 = vert26.replace(' ', '$');
            System.out.println(vert27);
        }
        else {
            System.out.println("Invalid Choice");
        }
    }
}
 
Convert Char to ASCII,Convert Character to ASCII,Java Convert Char to ASCII,Convert Char to ASCII in Java

that page may help you understand things better. each letter has a numeric number associated with it (ASCII code). It looks like you can cast a char diretcly to a int; thereby by giving the ascii code. The ascii codes follow order the same as alphas do, so if you add 14 to the int and cast it back to char, you'll receive the results you want.

Also, the link shows a different way of handling system input. try using bufferedreader to scan the lines.
 
Since Rot13 is completely pointless outside of school, I'm going to assume this is an assignment of some kind. With this in mind, we cannot tell you how to do it but I will give you the following advise.
1) Use OO to model the components of your application. It will help you understand the task and improve the end result. I used three classes but probably fewer lines of code.
2) Don't use 27 String variables named vert1-27
3) ASCII is not going to help you here. I doubt that it was an intentional red herring but if you haven't been told to use it as part of the assignment, leave it.
4) I'm flabbergasted you didn't notice it while you were working but decoding is exactly the same as encoding so why type it all out twice?

I know that it can be difficult to get over these early steps and I really want to help you out here.

1) You need to be iterating through the input String
2) You need to write a method (preferably in a different class) which can return you the rot13 encoded character if you give it the unencoded character. You don't need to "hard code" the conversions as you have done in your code, try to think about what the data "looks" like.

The above two points are all you need think about them carefully and post back here with your improved code.
 
Status
Not open for further replies.
Back
Top Bottom