如何在哈希图中打印新行“ \ n”?

时间:2019-04-22 00:49:28

标签: java arrays

我必须创建一个JavaFX应用程序,将字符串输入段转换为星号形式(8 * 6)矩阵。我已经编码以打印字符和空格。如何打印新行(\ n)?

我从textarea获得了输入段落,并使用“ \ n”将其分割了。我使用array通过使用object并返回输出来传递每一行。但是,仅显示最后一行,因为GC收集了前一行。

如果我使用错误的方式或者应该尝试其他方法,有人可以建议我吗?应该将整个段落用作输入内容吗?

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*/

预期结果:输入: 美国广播公司 防御 GHI 输出: 星号字母相同的字符串段落(多行):请参见下面的“星号字母”链接 LetterE in java with Asterisk

0 个答案:

没有答案