-3

I want to convert seven segment numbers to normal string in java. For example, if input string like this

input

   _  _     _  _  _  _  _  _   
 | _| _||_||_ |_   ||_||_|| |
 ||_  _|  | _||_|  ||_| _||_|

output should be like

1234567890

I have found this JavaScript answer, and I'm trying to convert it to java.

for now I have:

private static void get7segment(String ascii) 
    {
        String[] splited="909561432".split("");
        HashMap<Integer,Integer> map=new HashMap<Integer,Integer>();
        map.put(0, 63);
        map.put(1, 6);
        map.put(2, 91);
        map.put(3, 79);
        map.put(4, 102);
        map.put(5, 109);
        map.put(6, 125);
        map.put(7, 7);
        map.put(8, 127);
        map.put(9, 111);            
    }

any help would be appricheate

Devy
  • 703
  • 6
  • 17

2 Answers2

1

Based on Nina Scholz's idea:

public static void main(String[] args) {
String example= " _     _  _     _  _  _  _  _ \n| |  | _| _||_||_ |_   ||_||_| \n|_|  ||_  _|  | _||_|  ||_| _|";
        System.out.println(get7segment(example));
}

    private static String get7segment(String ascii) {
        String result = "";

        String[] lines = ascii.split("\n");
        String[] line1;
        String[] line2;
        String[] line3;

        for (int j = 0; j < lines.length - 2; j += 4) {
            line1 = lines[j].split("");
            line2 = lines[j + 1].split("");
            line3 = lines[j + 2].split("");

            String pow = "";
            int mod = 3;

            for (int i = 0; i < line1.length; i++) {
                if (i % mod == 0) {
                    String strAs = digitToString(pow);

                    result += strAs;
                    pow = "";
                }

                if (line1[i].equals("_") && i % mod == 1)
                    pow += "0";

                if (line2[i].equals("|") && i % mod == 0)// left
                    pow += "5";
                if (line2[i].equals("|") && i % mod == 2)// right
                    pow += "1";
                if (line2[i].equals("_") && i % mod == 1)// bottom
                    pow += "6";

                if (line3[i].equals("|") && i % mod == 0)// left
                    pow += "4";
                if (line3[i].equals("|") && i % mod == 2)// right
                    pow += "2";
                if (line3[i].equals("_") && i % mod == 1)// bottom
                    pow += "3";

                if (line1.length - 1 == i) {
                    String strAs = digitToString(pow);
                    result += strAs;
                    pow = "";
                }
            }


            result += "\n";
        }
        return result;
    }

    /*
     * Converting single ascii digit to regular digit
     */
    private static String digitToString(String asciiDigit) {
        if (asciiDigit == null || asciiDigit.equals(""))
            return "";
        int pow = 0;
        for (int i = 0; i < asciiDigit.length(); i++)
            pow += Math.pow(2, Character.getNumericValue(asciiDigit.charAt(i)));

        HashMap<Integer, Integer> map = new HashMap<Integer, Integer>() {
            {
                put(63, 0);
                put(6, 1);
                put(91, 2);
                put(79, 3);
                put(102, 4);
                put(109, 5);
                put(125, 6);
                put(7, 7);
                put(127, 8);
                put(111, 9);
            }
        };
        return map.containsKey(pow) ? Integer.toString(map.get(pow)) : "?";
    }

Output:

0123456789

Nehorai Elbaz
  • 2,412
  • 8
  • 17
0

Attempting to parse a three line string into a sequence of numbers is going to be tricky w/o some sort of delimiter between the numbers.

123456789012345678901234567890
..._.._....._.._.._.._.._.._..
.|._|._||_||_.|_...||_||_||.|.
.||_.._|..|._||_|..||_|._||_|.
123456789012345678901234567890

Above is your original sample with the spaces replaced by dots to highlight them. Notice that some digits are 1 character wide ('1'), some are two ('3', '7'), and the rest are three.

Questions: - Are '1's always going to only take two columns? - There's a space between '2' and '3' and between '6' and '7', but not between the other digits. There's also a leading space before the '1'. Is the rule that characters requiring less than three columns will have a leading space? - Will there ever be other spaces?

The way I would do this would be to craft an object taking three Strings (the three lines). Strings would have to be of equal length. Then, implement a char-wise parser that pulls characters off the three strings equally. If all three chars are a space, drop them. If not, read a column, do you have a '1'? If not, read another, do you have a '3' or '7'? If not, read a third, what character do you have?

Does that help?

toddg
  • 141
  • 6
  • Of course, answering the question "do you have a particular digit" can be done using the pattern technique suggested in the referenced javascript answer. Namely: construct a hash value using the characters across all three lines and compare that to constants for each character. – toddg Sep 18 '19 at 17:10