Java Unicode到TextArea

时间:2013-05-21 15:54:26

标签: java unicode jtextarea

好的,所以我尝试了将unicode硬编码到我的程序中的所有内容,但我的条件语句没有读到在TextArea中匹配√-。我正在编写一个计算器程序,我希望Java将其读作NaN。当我使用TextArea本身时,它只会跳过我的其他if语句。我在没有TextArea的情况下测试了它,但是我得到了NaN,但是返回了TextArea中使用的数字。

例如:

测试程序(没有GUI) - >运行完美的输出NaN

String Text = "√-25";
System.out.println(Text);
ArrayList<String> OP = new ArrayList();
ArrayList<Float> NUM = new ArrayList();
Scanner OPscan = new Scanner(Text).useDelimiter("[[.][0-9]]+");
Scanner NUMscan = new Scanner(Text).useDelimiter("[-+*/√]+");
int iOP = 0;
int iNUM = 0;
float Root = 0;
while (OPscan.hasNext()) {
    OP.add(OPscan.next());
}
OPscan.close();
System.out.println(OP + "OP Size: " + OP.size());

while (NUMscan.hasNextFloat()) {
    if (OP.get(iOP).equals("-")) {
        NUM.add(-NUMscan.nextFloat());
        OP.set(iOP, "+");
    } else if (OP.get(iOP).equals("--")) {
        NUM.add(-NUMscan.nextFloat());
        OP.set(iOP, "-");
    } else if (OP.get(iOP).equals("+-")) {
        NUM.add(-NUMscan.nextFloat());
        OP.set(iOP, "+");
    } else if (OP.get(iOP).equals("*-")) {
        NUM.add(-NUMscan.nextFloat());
        OP.set(iOP, "*");
    } else if (OP.get(iOP).equals("/-")) {
        NUM.add(-NUMscan.nextFloat());
        OP.set(iOP, "/");
    } else if (OP.get(iOP).equals("√-")) {
        NUM.add(-NUMscan.nextFloat());
        OP.set(iOP, "√");
    } else {
        NUM.add(NUMscan.nextFloat());
    }
    iOP++;
}
System.out.println(NUM + "NUM Size: " + NUM.size());
System.out.println(OP + "NUM Size: " + NUM.size());

while (OP.contains("√")) {
    try {
        if (OP.get(iOP).equals("√")) {
            Root = (float) Math.sqrt(NUM.get(iNUM));
            NUM.set(iNUM, Root);
            OP.remove(iOP);

            System.out.println(Root + " Root!");
        }

        if (OP.get(0).matches("[+-*/]+")) {
            iOP++;
            iNUM++;
        }
    } catch (IndexOutOfBoundsException IndexOutOfBoundsException) {
        System.out.println("Index Error Bypassed! " + "INDEX: " + "iOP:" + iOP + " iNUM:" + iNUM + " | Size: " + "iOP:" + OP.size() + " iNUM:" + NUM.size());
        iOP = 0;
        iNUM = 0;
    }
}

使用GUI&amp; amp; TextArea输出 - &gt;只有25

1 个答案:

答案 0 :(得分:2)

使用

Unicode representation
\u221A

e.g。

public static void main(String[] args) {
        System.out.println("Encoding: " + System.getProperty("file.encoding"));
        JTextArea area = new JTextArea(10, 30);
        JScrollPane pane = new JScrollPane(area);
        JOptionPane.showMessageDialog(null, pane);
        String text = area.getText();
        char sqrt = '\u221A';
        if (text.contains(Character.toString (sqrt))) {
            System.out.println("YES for " + text);
        } else {
            System.out.println("NO for " + text);
        }
    }