Search code examples
javaeclipserot13

ROT13 in Java doesn´t want to work properly


At some point I have trouble programming ROT13 in Java. So the User shall write whatever he wants and the programm should rewrite it in ROT13. So here´s my programm until now:

import java.io.*;


public class rot13

{

public static void main (String [] args) throws IOException
    {
    BufferedReader myInput = new BufferedReader (new InputStreamReader (System.in));// Buffered Reader reads the number inputed 

    String key [] = {"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"};
    String keyA [] = {"N","O","P","Q","R","S","T","U","V","W","X","Y","Z","A","B","C","D","E","F","G","H","I","J","K","L","M"};
    String letter;

    System.out.println("Enter a phrase:");
    String phrase = myInput.readLine();
    int y = 0, i = 0;
    while ( y <= phrase.length()){

        letter = Character.toString(phrase.charAt(y));

        while(i <= y){

            if (letter != key[i]){
                keyA [i] = keyA[i];
            }
            i++;
        }
        System.out.println(keyA [i]);
        y++;
        } 
    }
}

The problem is the following: It only does go for a few letters, but stops working after like 3 lines or rather after 3 latters and puts up errors which are:

Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 3
    at java.lang.String.charAt(Unknown Source)
    at rot13.main(rot13.java:19)

I´ve tried different words, but it keeps printing out the same problem. Does anyone knows how to fix it or at least a way to do it more proberly?

Thanks in advance!!


Solution

  • Why it doesn't work

    import java.io.*;
    
    
    public class rot13
    
    {
    
    public static void main (String [] args) throws IOException
    {
    BufferedReader myInput = new BufferedReader (new InputStreamReader (System.in));// Buffered Reader reads the number inputed 
    
    String key [] = {"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"};
    String keyA [] = {"N","O","P","Q","R","S","T","U","V","W","X","Y","Z","A","B","C","D","E","F","G","H","I","J","K","L","M"};
    String letter;
    
    System.out.println("Enter a phrase:");
    String phrase = myInput.readLine();
    int y = 0, i = 0;
    while ( y <= phrase.length()){
    
        letter = Character.toString(phrase.charAt(y));
        //Each time you go throught the first loop, you are comparing your actual position in the string and i
        //But as you don't reset i back to 0, you only try to compare your previous index and your actual index : if y == 3, so i takes only the values 2 and 3
        //Moreover, when y > 26, you try to access the key array outside of its bounds 
        while(i <= y){
            // letter is a string so you should be using equals
            if (letter != key[i]){
                // You are putting the value at the i index in the i index, so you do basically nothing with this line
                keyA [i] = keyA[i];
            }
            i++;
        }
        System.out.println(keyA [i]);
        y++;
        } 
    }
    

    }

    Alternative

    Here's a solution you can use :

    import java.io.*;
    
    
    public class rot13 {
    
    public static void main(String[] args) throws IOException {
        BufferedReader myInput = new BufferedReader(new InputStreamReader(System.in));// Buffered Reader reads the number inputed
        System.out.println("Enter a phrase:");
        String input = myInput.readLine();
    
        //We loop through every char in the string
        for (char c : input.toCharArray()) {
            //We check if the character is a letter, so we don't add the offset to special characters like space or dot
            if (Character.isAlphabetic(c)) {
                //Here we get the lower case version of the char and remove it 97, which is the ascii value of 'a'
                //With this, we are mapping letters from a to z to numbers from 0 to 25
                char lowerChar = (char) (Character.toLowerCase(c) - 97);
                //We add the offset of 13
                lowerChar += 13;
                //We then use the modulo to move numbers higher than 15 back to the beginning
                lowerChar %= 26;
                //We finally come back to the ascii value of our lower case char
                lowerChar += 97;
                System.out.print(Character.isUpperCase(c) ? Character.toUpperCase(lowerChar) : lowerChar);
            } else {
                //If it's not a letter, we just print the char
                System.out.print(c);
            }
        }
    
        //We don't forget to close our BuffererReader
        myInput.close();
    }
    }
    

    This is a described version but you can shorten it by doing all the char operations on one line