Ascii Art String to Character conversion

2019-06-08 16:32发布

问题:

I have an input as Ascii art design and i want the real character to be printed based on the inputted ascii art. I have created the below program but i dont whats wrong with it.

     import java.util.*;

 class Ascii {
    static final char START_CHAR = 'a';
    static final char END_CHAR = 'z';
    static final char DELIMITER_CHAR = END_CHAR + 1;

    public static String printchar(char c){
       int l =5,w=4,start=0,end=0;
        String v="";
       c=Character.toLowerCase(c);

       String[] rowArray = new String[5];
       rowArray[0]=" #  ##   ## ##  ### ###  ## # # ###  ## # # #   # # ###  #  ##   #  ##   ## ### # # # # # # # # # # ### ###"; 
       rowArray[1]="# # # # #   # # #   #   #   # #  #    # # # #   ### # # # # # # # # # # #    #  # # # # # # # # # #   #   #"; 
       rowArray[2]="### ##  #   # # ##  ##  # # ###  #    # ##  #   ### # # # # ##  # # ##   #   #  # # # # ###  #   #   #   ##"; 
       rowArray[3]="# # # # #   # # #   #   # # # #  #  # # # # #   # # # # # # #    ## # #   #  #  # # # # ### # #  #  #      ";  
       rowArray[4]="# # ##   ## ##  ### #    ## # # ###  #  # # ### # # # #  #  #     # # # ##   #  ###  #  # # # #  #  ###  # ";   

      if(START_CHAR <= c && c <= END_CHAR)
      {
        start = (c-START_CHAR)* w;
        end = start+w;
      }

      else
      {
          start=103;end=107;

      }

         for (int i = 0; i < l; i++) {
              v = v+"\n"+rowArray[i].substring(start,end);
          }
    return v;

    }
}

public class Solution {
public static void main(String args[]) {

    String b = Ascii.printchar('A');
    System.out.println(b);  
    char c = A.scanChar(b);
    System.out.println("Corresponding Letter ="+c);
        }
}

class A{

static char scanChar(String s)
{
    Ascii.
  Map<String,Character> mapping= new HashMap<>(26);
      mapping.put(" # \n# #\n###\n# #\n# #\n", 'A');
      mapping.put("## \n# #\n## \n# #\n##\n", 'B');
      mapping.put(" ##\n#  \n#  \n#  \n ##\n", 'C');
      mapping.put("## \n# #\n# #\n# #\n## \n", 'D');
      mapping.put("###\n#  \n## \n#  \n###\n", 'E');
      mapping.put(" ###\n#  \n## \n#  \n#   \n", 'F');
      mapping.put("###\n  #\n ##\n   \n # \n", '?');
      System.out.println(mapping.get('A'));
     return mapping.get(s);


    }

    }

Can somebody please point out the error as of now i am getting null when comparing the string with the map value ?

回答1:

The docs for the get() method specify that

Returns the value to which the specified key is mapped, or null if this map contains no mapping for the key.

You are putting the ASCII art as the key and not the value. So when you try to get the key of 'A' (There is none) it returns null.

Change:

  mapping.put(" # \n# #\n###\n# #\n# #\n", 'A');
  mapping.put("## \n# #\n## \n# #\n##\n", 'B');
  mapping.put(" ##\n#  \n#  \n#  \n ##\n", 'C');
  mapping.put("## \n# #\n# #\n# #\n## \n", 'D');
  mapping.put("###\n#  \n## \n#  \n###\n", 'E');
  mapping.put(" ###\n#  \n## \n#  \n#   \n", 'F');
  mapping.put("###\n  #\n ##\n   \n # \n", '?');

To:

  mapping.put('A', " # \n# #\n###\n# #\n# #\n");
  mapping.put('B' , "## \n# #\n## \n# #\n##\n");
  mapping.put('C', " ##\n#  \n#  \n#  \n ##\n");
  mapping.put('D', "## \n# #\n# #\n# #\n## \n");
  mapping.put('E', "###\n#  \n## \n#  \n###\n");
  mapping.put('F', " ###\n#  \n## \n#  \n#   \n");
  mapping.put('?', "###\n  #\n ##\n   \n # \n");


回答2:

 import java.util.*;

 class Ascii {
    static final char START_CHAR = 'a';
    static final char END_CHAR = 'z';
    static final char DELIMITER_CHAR = END_CHAR + 1;

    public static String printchar(char c){
       int l =5,w=4,start=0,end=0;
        String v="";
       c=Character.toLowerCase(c);

       String[] rowArray = new String[5];
       rowArray[0]=" #  ##   ## ##  ### ###  ## # # ###  ## # # #   # # ###  #  ##   #  ##   ## ### # # # # # # # # # # ### ###"; 
       rowArray[1]="# # # # #   # # #   #   #   # #  #    # # # #   ### # # # # # # # # # # #    #  # # # # # # # # # #   #   #"; 
       rowArray[2]="### ##  #   # # ##  ##  # # ###  #    # ##  #   ### # # # # ##  # # ##   #   #  # # # # ###  #   #   #   ##"; 
       rowArray[3]="# # # # #   # # #   #   # # # #  #  # # # # #   # # # # # # #    ## # #   #  #  # # # # ### # #  #  #      ";  
       rowArray[4]="# # ##   ## ##  ### #    ## # # ###  #  # # ### # # # #  #  #     # # # ##   #  ###  #  # # # #  #  ###  # ";   

      if(START_CHAR <= c && c <= END_CHAR)
      {
        start = (c-START_CHAR)* w;
        end = start+w;
      }

      else
      {
          start=103;end=107;

      }

         for (int i = 0; i < l; i++) {
              v = v+"\n"+rowArray[i].substring(start,end);
          }
    return v;

    }
}

public class Solution {
public static void main(String args[]) {

    String b = Ascii.printchar('Z');
    System.out.println(b);  
    System.out.println();
    char c = A.scanChar(b);
    System.out.println("Corresponding Letter = "+c);
        }
}

class A{

static char scanChar(String s)
{
    //s= s.trim();
    Character key=null;
  Map<Character,String> mapping= new HashMap<>();
  mapping.put('A',"\n #  \n# # \n### \n# # \n# # ");
  mapping.put('B',"\n##  \n# # \n##  \n# # \n##  ");
  mapping.put('C',"\n ## \n#   \n#   \n#   \n ## ");
  mapping.put('D',"\n##  \n# # \n# # \n# # \n##  ");
  mapping.put('E',"\n### \n#   \n##  \n#   \n### ");
  mapping.put('F',"\n### \n#   \n##  \n#   \n#   ");
  mapping.put('G',"\n ## \n#   \n# # \n# # \n ## ");
  mapping.put('H',"\n# # \n# # \n### \n# # \n# # ");
  mapping.put('I',"\n### \n #  \n #  \n #  \n### ");
  mapping.put('J',"\n ## \n  # \n  # \n# # \n #  ");
  mapping.put('K',"\n# # \n# # \n##  \n# # \n# # ");
  mapping.put('L',"\n#   \n#   \n#   \n#   \n### ");
  mapping.put('M',"\n# # \n### \n### \n# # \n# # ");
  mapping.put('N',"\n### \n# # \n# # \n# # \n# # ");
  mapping.put('O',"\n #  \n# # \n# # \n# # \n #  ");
  mapping.put('P',"\n##  \n# # \n##  \n#   \n#   ");
  mapping.put('Q',"\n #  \n# # \n# # \n ## \n  # ");
  mapping.put('R',"\n##  \n# # \n##  \n# # \n# # ");
  mapping.put('S',"\n ## \n#   \n #  \n  # \n##  ");
  mapping.put('T',"\n### \n #  \n #  \n #  \n #  ");
  mapping.put('U',"\n# # \n# # \n# # \n# # \n### ");
  mapping.put('V',"\n# # \n# # \n# # \n# # \n #  ");
  mapping.put('W',"\n# # \n# # \n### \n### \n# # ");
  mapping.put('X',"\n# # \n# # \n #  \n# # \n# # ");
  mapping.put('Y',"\n# # \n# # \n #  \n #  \n #  ");
  mapping.put('Z',"\n### \n  # \n #  \n#   \n### ");
     /*char[] ct = new char[s.length()];
     ct= s.toCharArray();
     String l = "\n# # \n# # \n### \n# # \n# # ";
     for(char c:ct)
     System.out.print(c);

     System.out.println();
     System.out.println("Length of L ="+l.length());
     System.out.println("Length of S ="+s.length());
     System.out.println("Equality ="+s.equals(l));
     */
     for(Map.Entry entry: mapping.entrySet())
     {
         if(s.equals(entry.getValue()))
         {
          key = (char)entry.getKey();
          break;
         }
         else{
             key='?';
         }
     }

     return key;

}
}

Made the solution finally.