I have a java binary search tree and I want to create a menu.
To this day I used StreamTokenizer to get the user input,
But now it doesn't seem to work with "+"
, "-"
, "?"
.
My code:
public void listen() throws IOException {
boolean stay = true;
System.out.println("Give me commands .. ");
while(stay) {
tokens.nextToken();
if(tokens.sval.equals("+")) {
tree.insert(new PositiveInt((int) tokens.nval));
} else if(tokens.sval.equals("?")) {
System.out.println(
tree.retrieve(new PositiveInt((int) tokens.nval)) == null ? "Not exist" : "exist");
} else if(tokens.sval.equals("-")) {
tree.remove(new PositiveInt((int) tokens.nval));
} else if(tokens.sval.equalsIgnoreCase("K")) {
tree.writeKeys();
} else if(tokens.sval.equalsIgnoreCase("E")) {
System.out.println("Empty = " + tree.isEmpty());
} else if(tokens.sval.equalsIgnoreCase("F")) {
System.out.println("Full = " + tree.isFull());
} else if(tokens.sval.equalsIgnoreCase("C")) {
tree.clear();
} else if(tokens.sval.equalsIgnoreCase("P")) {
tree.showStructure();
} else if(tokens.sval.equalsIgnoreCase("Q")) {
stay = false;
} else {
System.out.println("Unaccaptable input.");
}
}
}
When I enter "P" , for example, or any other character, everything's alright.
When I enter "?"
, "+"
, "-"
, I'm getting:
Exception in thread "main" java.lang.NullPointerException
at TestBSTree.listen(TestBSTree.java:27)
at TestBSTree.main(TestBSTree.java:54)
As Line 27 is :
if(tokens.sval.equals("+")) {
In other words, a non-charater is not accaptable with the tokenizer.
Why and how can I fix it?
Whole code:
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StreamTokenizer;
public class TestBSTree {
// Test class variables
BSTree<PositiveInt> tree;
InputStreamReader reader;
StreamTokenizer tokens;
PositiveInt key;
int in;
public TestBSTree(PositiveInt root) {
tree = new BSTree<PositiveInt>(new BSTreeNode<>(root, null, null));
reader = new InputStreamReader(System.in);
tokens = new StreamTokenizer(reader);
key = null;
}
public void listen() throws IOException {
boolean stay = true;
System.out.println("Give me commands .. ");
while(stay) {
tokens.nextToken();
if(tokens.sval.equals("+")) {
tree.insert(new PositiveInt((int) tokens.nval));
} else if(tokens.sval.equals("?")) {
System.out.println(
tree.retrieve(new PositiveInt((int) tokens.nval)) == null ? "Not exist" : "exist");
} else if(tokens.sval.equals("-")) {
tree.remove(new PositiveInt((int) tokens.nval));
} else if(tokens.sval.equalsIgnoreCase("K")) {
tree.writeKeys();
} else if(tokens.sval.equalsIgnoreCase("E")) {
System.out.println("Empty = " + tree.isEmpty());
} else if(tokens.sval.equalsIgnoreCase("F")) {
System.out.println("Full = " + tree.isFull());
} else if(tokens.sval.equalsIgnoreCase("C")) {
tree.clear();
} else if(tokens.sval.equalsIgnoreCase("P")) {
tree.showStructure();
} else if(tokens.sval.equalsIgnoreCase("Q")) {
stay = false;
} else {
System.out.println("Unaccaptable input.");
}
}
}
public static void main(String[] args) throws IOException {
TestBSTree test = new TestBSTree(new PositiveInt(0));
test.listen();
}
}
- It doesn't matter how does the tree or PositiveInt implemented, the main issue is the tokenizer.
if you want so split a string containg a '?' or a plus ('+'), you cannot simply use this symbol to split this String; they are reserved 'words' and need a exclude sign '\' which itself needs an exclusion sign ^^ (so you need two '\\' and the special sign)
try to use something like that:
-> result: the count is 2 !
you can also apply this method for String.split("\+");
but this code will work!
it's the same 'problem' ^^ i hope this helped!
unfortunaltely i don't know which other symbols require a slahs... :-(