0

I am fairly new to programming in general and I was wondering how I could encode/decode text that is inputted.

All letters must be brought down 3 letters for ex A -> D B -> E and so forth

Ill put in some pseudocode for an example:

INPUT MESSAGE: "LORYHBRX"

Encoded Message:LORYHBRX

Decoded Message:ILOVEYOU

OUTPUT MESSAGE: "ILOVEYOU"

Please help.

So far I have

import java.util.*;

public class Encoder {

public static void main(String[] args) 
{   

    String a = "d";
    String b = "e";
    String c = "f";
    String d = "g";
    String e = "h";
    String f = "i";
    String g = "j";
    String h = "k";
    String i = "l";
    String j = "m";
    String k = "n";
    String l = "o";
    String m = "p";
    String n = "q";
    String o = "r";
    String p = "s";
    String q = "t";
    String r = "u";
    String s = "v";
    String t = "w";
    String u = "x";
    String v = "y";
    String w = "z";
    String x = "a";
    String y = "b";
    String z = "c";

    Scanner in = new Scanner(System.in);
    System.out.println("Please enter the text you wish to encode.");
    String place = in.nextLine();
    System.out.println(place);

}
}

I'm trying to convert what is inputted into the variables above.

I think I am stating all those strings as variable but I do not know how to make them changeable by the input.

Steven Jobs
  • 13
  • 1
  • 3
  • Start by writing code. And post questions when you're stuck with specific code issues. That is the only solution here. – CodeNewbie Sep 11 '14 at 06:08
  • I apologize. I put my code in. – Steven Jobs Sep 11 '14 at 06:15
  • Ugh! Storing the target alphabet as `String a = "d"` is not going to help you, because you cannot turn a letter from your string into a variable name. You need to learn about arrays and other container classes. For the task at hand, write a function that turns a source letter into a target letter. You can make use of the fact that the 26 letters form a contiguous block in ASCII. – M Oehm Sep 11 '14 at 06:32
  • BTW, the code you want is called a Caesar Cipher. Searching for that and the Java tag on SO should give you hints on how to attack this. – M Oehm Sep 11 '14 at 06:33
  • @StevenJobs it is impossible loryhbrx gives you iloveyou, but iloveyou gives you loryhbrx. if it is the case, let me know, so I can post up my answer :) – Kick Buttowski Sep 11 '14 at 07:05
  • @KickButtowski I would highly appreciate that, I can't thank all of you enough for dealing with a 2-bit coder like me. Thank you @M Oehm for the name of this "Caesar Cipher". – Steven Jobs Sep 11 '14 at 07:22
  • @stevenjobs am I through? it is impossible loryhbrx gives you iloveyou, but iloveyou gives you loryhbrx. – Kick Buttowski Sep 11 '14 at 07:23
  • @KickButtowski I need to make 2 programs, one that encodes a sentence and one that decodes a sentence. That is impossible? Like the first one can do A -> D and the second program can do D -> A – Steven Jobs Sep 11 '14 at 07:27
  • I sent you an email. Please help me asap. I appreciate it so much. – Steven Jobs Sep 11 '14 at 07:35
  • Anyone else can assist me with this? – Steven Jobs Sep 11 '14 at 07:37

2 Answers2

2

I will give you some hints how to do decoding and encoding will be just the reverse process.

First, you should know computer can only understand numbers which called Ascii code.

Ascii code is numerical representation of character like b , & , A , and so on, as result, capital and small letters have Ascii code which is int type.

Read and See Ascci table here

Another subject you should know is Casting

Casting is converting a type to another type like converting int to char or vice versa, but you should know casting some type cannot to convert to another type like type boolean to int which is impossible .

Read about casting in Java

Let talk about Encoding and leave decoding to you to figure it out because it is reverse of encoding.

Hints about how encode

  1. There is no need to define String like String a = "a";
  2. Since you learn about Ascci codes which are numbers, you can for loop through 97 to 122 with casting to get small chars.

    Example:

    System.out.println("Asci code of small a is " + (int)'a' + "\nsmall a is " + (char)97);
    

    output:

    Asci code of small a is 97
    small a is a
    

Note: you cast char to int and cast int to char.

  1. Since you are dealing with numbers, so you can do addition.

    Example:

    System.out.println("Three char after a is " +  (char)(97+3));
    

    Ouput:

    there char after a is d
    

for char x,y,and z,you can subtract 23 from their asci code , for example 120-23 is gonna give asci code for a.

Flexo
  • 87,323
  • 22
  • 191
  • 272
Kick Buttowski
  • 6,709
  • 13
  • 37
  • 58
0

The Whole Code of Encoding and Decoding

import java.util.Scanner;

public class coder {

    public static void main(String[] args) {
        String[] qe = {"1 ","2 ","3 ","4 ","5 ","6 ","7 ","8 ","9 ","10 ","11 ","12 ","13 ",
            "14 ","15 ","16 ","17 ","18 ","19 ","20 ","21 ","22 ","23 ","24 ","25 ","26 "};
        String [] eq = {"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"};
        Scanner in = new Scanner(System.in);
        System.out.println("Please enter the text you wish to encode.");
        String place = in.nextLine();
        place = place.toLowerCase();
        place = place.replace("a", qe[0]);
        place = place.replace("b", qe[1]);
        place = place.replace("c", qe[2]);
        place = place.replace("d", qe[3]);
        place = place.replace("e", qe[4]);
        place = place.replace("f", qe[5]);
        place = place.replace("g", qe[6]);
        place = place.replace("h", qe[7]);
        place = place.replace("i", qe[8]);
        place = place.replace("j", qe[9]);
        place = place.replace("k", qe[10]);
        place = place.replace("l", qe[11]);
        place = place.replace("m", qe[12]);
        place = place.replace("n", qe[13]);
        place = place.replace("o", qe[14]);
        place = place.replace("p", qe[15]);
        place = place.replace("q", qe[16]);
        place = place.replace("r", qe[17]);
        place = place.replace("s", qe[18]);
        place = place.replace("t", qe[19]);
        place = place.replace("u", qe[20]);
        place = place.replace("v", qe[21]);
        place = place.replace("w", qe[22]);
        place = place.replace("x", qe[23]);
        place = place.replace("y", qe[24]);
        place = place.replace("z", qe[25]);
        System.out.println(place);

        place = place.replace("26 ", eq[25]);
        place = place.replace("25 ", eq[24]);
        place = place.replace("24 ", eq[23]);
        place = place.replace("23 ", eq[22]);
        place = place.replace("22 ", eq[21]);
        place = place.replace("21 ", eq[20]);
        place = place.replace("20 ", eq[19]);
        place = place.replace("19 ", eq[18]);
        place = place.replace("18 ", eq[17]);
        place = place.replace("17 ", eq[16]);
        place = place.replace("16 ", eq[15]);
        place = place.replace("15 ", eq[14]);
        place = place.replace("14 ", eq[13]);
        place = place.replace("13 ", eq[12]);
        place = place.replace("12 ", eq[11]);
        place = place.replace("11 ", eq[10]);
        place = place.replace("10 ", eq[9]);
        place = place.replace("9 ", eq[8]);
        place = place.replace("8 ", eq[7]);
        place = place.replace("7 ", eq[6]);
        place = place.replace("6 ", eq[5]);
        place = place.replace("5 ", eq[4]);
        place = place.replace("4 ", eq[3]);
        place = place.replace("3 ", eq[2]);
        place = place.replace("2 ", eq[1]);
        place = place.replace("1 ", eq[0]);
        System.out.println(place);
    }

}
Matt Ke
  • 3,599
  • 12
  • 30
  • 49