Results 1 to 3 of 3
  1. #1
    Join Date
    Feb 2009
    Posts
    19
    Rep Power
    0

    Default Coded Message... Help Please

    I have a code that I need help with...
    ------
    Write a program that decodes secret messages read from a text file using an "alphabetic shift." So, for example, a shift might appear as:

    ORIGINAL ABCDE...XYZ
    SHIFTED. CDEFG...ZAB

    You must:

    1.) Read the encoded message from the attached MessageIn.txt file,
    2.) Count the instance of each character in the file,
    3.) The most common character in the message should be an "E". Use this information to calculate the amount of the shift,
    4.) Print out the decoded message.
    ----
    here is what i have to work with...
    the code that needs to be decoded:
    HVWGWGHVSPSGHQCADIHSFSLSFQWGSWVOJSSJSFSLDSFWSBQSR


    I have figured out that:
    char A in the message (encoded)= M (decoded);
    char B = N;
    char C = O;
    char D = P;
    char E = Q;
    char F = R;
    char G = S;
    char H = T;
    char I = U;
    char J = V;
    char K = X;
    char L = W;
    char M = Y;
    char N = Z;
    char O = A;
    char P = B;
    char Q = C;
    char R = D;
    char S = E;
    char T = F;
    char U = G;
    char V = H;
    char W = I;
    char X = J;
    char Y = K;
    char Z = L;
    ----
    I just need help finding out how to write it in java code... also here is what else I have to work with:
    :confused:
    public class test
    {
    public static void main(String args[])
    {
    //Method Number 1 using indexing
    String alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    int i = alpha.indexOf("G");

    //Method Number 2 using chars and ASCII values
    int j = (int)alpha.charAt(6) - (int)'A';

    System.out.println(i+" "+j);


    }

    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    You may wish to let Java do the figuring out for you. If it were me, I'd create a 26 item array of ints (I'd probably actually use a Map, but you haven't learned that yet, I'll bet), and use simple char math to count the number of times a char is present.

    For instance
    Java Code:
    // semi-pseudo code
    int[] charArray = new int[26];
    String inputString = // get input String from file...
    loop through every char in the input String
      char currentChar = inputString.charAt(i);
      int indexNumber = currentChar - 'A';
      charArray[indexNumber]++; // adds 1
    end Loop
    Last edited by Fubarable; 05-03-2009 at 08:22 PM.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Edit: this is not an applet question. I'm going to move this to the more appropriate "New to Java" section

Similar Threads

  1. Matrix Message
    By shindry in forum New To Java
    Replies: 2
    Last Post: 05-04-2009, 04:32 AM
  2. Scroll message
    By getkiran in forum Java Applets
    Replies: 1
    Last Post: 03-05-2009, 04:29 AM
  3. Color coded code
    By tim in forum Suggestions & Feedback
    Replies: 11
    Last Post: 06-29-2008, 09:35 AM
  4. Please wail Message in jsp
    By amar.java in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 04-02-2008, 12:29 PM
  5. simulate message box
    By pablitohh in forum New To Java
    Replies: 1
    Last Post: 12-05-2007, 04:52 PM

Posting Permissions

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