0

I have to create a JavaFX application that converts string input paragraph to asterisk form(8*6) matrix.I have coded to print characters and space. How can I print new line(\n)?

I have got input paragraph from textarea and split it using "\n". I used array to pass each line by using object and returned output. But only last line is displayed as previous ones are collected by GC.

Can anyone please suggest me if I am going in wrong way or should I try another method, should I take whole paragraph as input or something?

private void handleButtonAction(ActionEvent event) {
        txtOutput.clear(); //clear output field every time we run program
        String input = txtInput.getText();
        String[] lineArray = input.split("\n"); //split the text paragraph using new line
        for (int i=0; i<lineArray.length; i++) {

            Pattern obj = new Pattern(lineArray[i]);
            txtOutput.setText(obj.getOutput());
        }

        txtOutput.setPrefColumnCount(200);
    }
String getOutput() {
        char[] chars = input.toCharArray();
        //Character[][] result = new Character[CHAR_ROW_COUNT][(input.length() * CHAR_COLUMN_COUNT)];
        Character[][] result = new Character[CHAR_ROW_COUNT][(input.length() * CHAR_COLUMN_COUNT)];

        int resultColumnOffset = 0;
        //int noOfLines = 0;
        try {
            //for (int noOfLines :  )// run from 0 to total number of lines
            for (char c : chars) {
                Character[][] pattern = AsciiPattern.getPattern(c);
                for (int row = 0; row < pattern.length; row++) {
                    if (pattern[row].length >= 0)
                        System.arraycopy(pattern[row], 0, result[row], resultColumnOffset, pattern[row].length);
                }
                resultColumnOffset = resultColumnOffset + CHAR_COLUMN_COUNT; //starts printing character from offset/distance
            }
        } catch (NoSuchElementException nse) {
            return "invalid input " + input;
        }

        StringBuilder builder = new StringBuilder();
        for (Character[] characters : result) {
            for (Character character : characters) {
                builder.append(character);
                System.out.print(character);
            }
            builder.append("\n");
            System.out.println();
        }

        return builder.toString();
    }
}
class AsciiPattern {
    private static final Map<Character, Character[][]> map = new HashMap<>();

    static {
        map.put(' ', new Character[][]{ 
            {' ', ' ', ' ', ' ', ' ', ' ',},
            {' ', ' ', ' ', ' ', ' ', ' ',},
            {' ', ' ', ' ', ' ', ' ', ' ',},
            {' ', ' ', ' ', ' ', ' ', ' ',},
            {' ', ' ', ' ', ' ', ' ', ' ',},
            {' ', ' ', ' ', ' ', ' ', ' ',},
            {' ', ' ', ' ', ' ', ' ', ' ',},
            {' ', ' ', ' ', ' ', ' ', ' ',},
        });
        map.put('A', new Character[][]{ 
            {' ', '*', '*', '*', ' ', ' '},
            {'*', ' ', ' ', ' ', '*', ' '},
            {'*', ' ', ' ', ' ', '*', ' '},
            {'*', ' ', ' ', ' ', '*', ' '},
            {'*', '*', '*', '*', '*', ' '},
            {'*', ' ', ' ', ' ', '*', ' '},
            {'*', ' ', ' ', ' ', '*', ' '},
            {'*', ' ', ' ', ' ', '*', ' '}
        });

/*code for other letter goes here*/

Expected result: Input: ABC DEF GHI Output: same string paragraph(multiple lines) in asterisk letters: see below link for 'asterisk letter' LetterE in java with Asterisk

Navdha Y
  • 3
  • 1

0 Answers0