ENCODER-DECODER:解码器不接受大字符串值

时间:2014-10-07 20:06:50

标签: java binary bluej encoder decoder

其实我只是为了好玩而制作这个节目。它包括编码器和解码器。编码器的工作原理如下:

  1. 用户将输入一个字符串
  2. 它将从字符串
  3. 逐个取出每个字符
  4. 它会将ASCII等价物存储在变量
  5. 将生成此ASCII值的二进制等值
  6. 然后它会以“dots('。')”和“spaces('')”
  7. 的形式打印这个二进制等价物
  8. 每个“一个('1')都有一个DOT”,每个“零('0')都有一个空格”
  9. 解码器以相反的方式工作。它从用户获取这些代码,它以点和空格的形式出现,并打印相应的字符串。由于我正在处理的字符的ASCII值范围从32(空格)到126(〜),因此二进制等效项的最大长度可以是7.因此,如果二进制等效项的长度小于7,则编码器将在二进制等效之前自动添加所需的零数量,使长度为7,这样解码器就可以轻松解码。解码器适用于小句子的代码。但是当我尝试输入一个长句子的代码(比如我正在写的那个)时,它在Bluej中不接受但在命令提示符中接受它;任何人都可以告诉我为什么会这样。大家请在命令提示符或任何你使用的程序上尝试这个程序。请提出任何建议以改进我的计划并使代码更复杂。

    这是代码:

    import java.io.*;
    
    class ed2 {
    
        BufferedReader obj = new BufferedReader(new InputStreamReader(System.in));
    
        void encode() throws InterruptedException, IOException {
            String rs, ren;
    
            encoder_symbol();
            rs = encode_input();
            ren = encode_find(rs);
            encode_display(ren);
        }
    
        void decode() throws InterruptedException, IOException {
            String rs, rde;
    
            decoder_symbol();
            rs = decode_input();
            rde = decode_find(rs);
            decode_display(rde);
        }
    
        void encoder_symbol() throws InterruptedException // just for fun
        {
            System.out.println("********  ***         ***  *********  ************  ******      ********  *****");
            Thread.sleep(100);
            System.out.println("********  ****        ***  *********  ************  ********    ********  *** **");
            Thread.sleep(100);
            System.out.println("***       *****       ***  ***        ***      ***  ***   ***   ***       ***  **");
            Thread.sleep(100);
            System.out.println("***       *** **      ***  ***        ***      ***  ***    ***  ***       *** **");
            Thread.sleep(100);
            System.out.println("******    ***  **     ***  ***        ***      ***  ***    ***  ******    *****");
            Thread.sleep(100);
            System.out.println("******    ***   **    ***  ***        ***      ***  ***    ***  ******    *****");
            Thread.sleep(100);
            System.out.println("***       ***     **  ***  ***        ***      ***  ***    ***  ***       *** **");
            Thread.sleep(100);
            System.out.println("***       ***      ** ***  ***        ***      ***  ***   ***   ***       ***  **");
            Thread.sleep(100);
            System.out.println("*******   ***       *****  *********  ************  ********    ********  ***   **");
            Thread.sleep(100);
            System.out.println("*******   ***        ****  *********  ************  ******      ********  ***    **");
            Thread.sleep(2700);
    
            System.out.println();
            System.out.println();
        }
    
        void decoder_symbol() throws InterruptedException // just for fun
        {
            System.out.println("******      ********  *********  ************  ******      ********  *****");
            Thread.sleep(100);
            System.out.println("********    ********  *********  ************  ********    ********  *** **");
            Thread.sleep(100);
            System.out.println("***   ***   ***       ***        ***      ***  ***   ***   ***       ***  **");
            Thread.sleep(100);
            System.out.println("***    ***  ***       ***        ***      ***  ***    ***  ***       *** **");
            Thread.sleep(100);
            System.out.println("***    ***  ******    ***        ***      ***  ***    ***  ******    *****");
            Thread.sleep(100);
            System.out.println("***    ***  ******    ***        ***      ***  ***    ***  ******    *****");
            Thread.sleep(100);
            System.out.println("***    ***  ***       ***        ***      ***  ***    ***  ***       *** **");
            Thread.sleep(100);
            System.out.println("***   ***   ***       ***        ***      ***  ***   ***   ***       ***  **");
            Thread.sleep(100);
            System.out.println("********    ********  *********  ************  ********    ********  ***   **");
            Thread.sleep(100);
            System.out.println("******      ********  *********  ************  ******      ********  ***    **");
            Thread.sleep(1000);
    
            System.out.println();
            System.out.println();
        }
    
        String encode_input() throws IOException {
            String s;
            System.out.println("ENTER THE STRING TO BE ENCODED");
            s = obj.readLine();
            return (s);
        }
    
        String decode_input() throws IOException {
            String s;
            System.out.println("ENTER THE CODE TO BE DECODED");
            s = obj.readLine();
            return (s);
        }
    
        String encode_find(String s)// converting the string into its binary
                                    // equivalent
        {
            int ac, i, j, l, chklen;
            String bc, en = "";
            char ic;
            l = s.length();
            for (i = 0; i < l; i++) {
                ic = s.charAt(i); // takes out every character
                bc = "";
                ac = (int) ic; // ASCII value of this character
                while (ac != 0) {
                    bc = Integer.toString((ac % 2)) + bc; // converting the ASCII
                                                          // value into binary
                                                          // equivalent
                    ac = ac / 2;
                }
                chklen = bc.length();// length of the binary equivalent
                if (chklen < 7) {
                    for (j = 1; j <= (7 - chklen); j++) // increasing the length of
                                                        // binary equivalent so that
                                                        // it becomes equal to 7
                    {
                        bc = "0" + bc;
                    }
                }
                en = en + bc; // concatenating all the binary equivalent into one
                              // string
            }
            return (en);
        }
    
        String decode_find(String s)// converts binary(i.e. in the form of dots and
                                    // space) to decimal
        {
            int f;// for the index of every character of code
            long l, i, j, ac;
            char c;
            String de = "";
    
            l = s.length();
            f = 0;// index of first caharcter
            for (i = 0; i < (l / 7); i++)// since the length of every binary
                                         // equivalent of a character is 7 therefore
                                         // there will be (length/7) characters in a
                                         // code of length l
            {
                ac = 0;// intializes the decimal(ASCII) equivalent to zero
                for (j = 6; j >= 0; j--)// loop will work 7 times for every binary
                                        // equivalent of a character
                {
                    c = s.charAt(f);// takes out every dot or space
                    if (c == '.')// it means that c corresponds to 'one'
                    {
                        ac = ac + ((int) Math.pow(2, j));// converting binary into
                                                         // decimal(ASCII)
                                                         // equivalent by adding all
                                                         // the powers of 2 which
                                                         // correspond to one('.')
                    }
                    f++;// increasing the index for next character of binary
                        // equivalent
                }
                de = de + ((char) ac);// converts the ASCII equivalent into
                                      // character and then concatenates it with the
                                      // intitial string
            }
            return (de);
        }
    
        void encode_display(String en)// displays the code
        {
            int i, l;
            char ic;
            System.out.println("YOUR ENCODED MESSAGE IS :");
            l = en.length();
            for (i = 0; i < l; i++) {
                ic = en.charAt(i);
                if (ic == '1')// for every 'one' it will print '.'(dot)
                {
                    System.out.print(".");
                } else if (ic == '0')// for every 'zero' it will print ' '(space)
                {
                    System.out.print(" ");
                }
            }
        }
    
        void decode_display(String de) {
            System.out.println(de);
        }
    
        public static void main(String args[]) throws IOException, InterruptedException {
            BufferedReader obj = new BufferedReader(new InputStreamReader(System.in));
    
            char ch;
    
            ed2 ed = new ed2();
    
            System.out.println("PRESS 'E' TO ENCODE A MESSAGE OR PRESS 'D' TO DECODE A MESSAGE");
            ch = (char) obj.read();
    
            if ((ch == 'e') || (ch == 'E')) {
                ed.encode();
            } else if ((ch == 'd') || (ch == 'D')) {
                ed.decode();
            }
        }
    }
    

0 个答案:

没有答案