Java Unicode to TextArea -
ok, have tried hard coding unicode program, conditional statement won't read √-
matched in textarea. i'm writing calculator program , want java read nan. skips on else if statement when i'm using textarea itself. tested without textarea , nan, returns number used in textarea.
for example:
test program (without gui) --> runs fine outputs 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; } }
program gui & textarea outputs --> 25
use unicode representation of √
\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 " + text); } else { system.out.println("no " + text); } }
Comments
Post a Comment