简体   繁体   中英

How to create an encoder? Decoder?

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 -> DB -> 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.

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.

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);
    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM