Ok, so I have tried everything even hard coding unicode into my program, but my conditional statement won't read that a √-
was matched in the TextArea. I'm writing a calculator program and I want Java to read it as NaN. It only skips over my else if statement when I'm using the TextArea itself. I tested it without the TextArea and I get NaN, but returns the number used in the TextArea.
For Example:
Test Program (without GUI) --> Runs perfectly 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 with GUI & TextArea outputs --> just 25
Use the 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 for " + text);
} else {
System.out.println("NO for " + text);
}
}