Results 1 to 4 of 4
Like Tree2Likes
  • 1 Post By masijade
  • 1 Post By gimbal2

Thread: Java Program: ROT13 Encryption help

  1. #1
    incxx is offline Member
    Join Date
    Mar 2013
    Posts
    21
    Rep Power
    0

    Default Java Program: ROT13 Encryption help

    The program allows the user to enter a phrase, and the program is supposed to print the phrase using ROT13 , which replaces each letter with the letter 13 places forward, using arrays to store the characters. This is what I have so far but it doesn't work. When I run the code, after you enter a phrase, It will output letters beginning from "O" and for the length of the phrase. Why is this happening?

    XML Code:
    import java.io.*;
    
    public class program
    {
    
    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++;
    } 
    }
    }

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default Re: Java Program: ROT13 Encryption help

    Must you use arrays, or can you use lists?

    In either case I would only use 1 list or array and do it as follows:

    1. retrieve the index of the entered char
    2. add 13 to that index value
    3. subtract the length of the list from that index value if that index value is greater than or equal to the length of the list
    4. retrieve the value at this new index from the list.
    gimbal2 likes this.

  3. #3
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,995
    Rep Power
    5

    Default Re: Java Program: ROT13 Encryption help

    I also spot this in the code at line 24:

    Java Code:
    keyA [i] = keyA[i];
    Pretty sure that this is not what was intended.
    masijade likes this.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  4. #4
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,519
    Blog Entries
    7
    Rep Power
    20

    Default Re: Java Program: ROT13 Encryption help

    Why not implement that rot13 en/decoding in just a bit of code? We don't need 52 Strings and 2 String arrays for that ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Simple Encryption Program
    By yasarqamar in forum New To Java
    Replies: 8
    Last Post: 07-23-2012, 12:56 PM
  2. Java encryption
    By inusc in forum New To Java
    Replies: 0
    Last Post: 09-13-2011, 04:34 PM
  3. Help in Java Encryption
    By wtver87 in forum Advanced Java
    Replies: 3
    Last Post: 04-24-2008, 06:11 AM
  4. Java encryption
    By soul_krasty in forum Advanced Java
    Replies: 3
    Last Post: 08-14-2007, 02:44 PM
  5. Encryption in java
    By Ed in forum Advanced Java
    Replies: 1
    Last Post: 07-09-2007, 01:47 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •