I'm working on a priority queue for my homework and I need to call on methods from another class to identify if a word starts with a vowel, letter , or other character. I have tested them inside and outside the class but it only seems to not work when I input a temp variable that uses toString() to convert and object into a string. For some reason the startsWithVowel always returns false and startsWithLetter always returns true.
public static boolean startsWithVowel(String input) {
char W = input.charAt(0);
if ((W == 'a') || (W == 'e') || (W == 'i') || (W == 'o') || (W == 'u') || (W == 'A') || (W == 'E') || (W == 'I') || (W == 'O') || (W == 'U')) {
return true;
}
else {
return false;
}
}
public static boolean startsWithLetter(String input) {
char W = input.charAt(0);
if ((W == 'a')|| (W == 'b') || (W == 'c') || (W == 'd') || (W == 'e') || (W == 'f') || (W == 'g') || (W == 'h') || (W == 'i') || (W == 'j') || (W == 'k') || (W == 'l') || (W == 'm') || (W == 'n') || (W == 'o') || (W == 'p') || (W == 'q') || (W == 'r') || (W == 's') || (W == 't') || (W == 'u') || (W == 'v') || (W == 'w') || (W == 'x') || (W == 'y') || (W == 'z') || (W == 'A')|| (W == 'B') || (W == 'C') || (W == 'D') || (W == 'E') || (W == 'F') || (W == 'G') || (W == 'H') || (W == 'I') || (W == 'J') || (W == 'K') || (W == 'L') || (W == 'M') || (W == 'N') || (W == 'O') || (W == 'P') || (W == 'Q') || (W == 'R') || (W == 'S') || (W == 'T') || (W == 'U') || (W == 'V') || (W == 'W') || (W == 'X') || (W == 'Y') || (W == 'Z')) {
return true;
}
else {
return false;
}
}
public void enqueue(Word newEntry,int prio) {
Node<Word> newNode = new Node<Word>(newEntry,prio);
String temp = newNode.toString();
if (Words.startsWithLetter(temp)) {
if (Words.startsWithVowel(temp)) {
newNode.setPriority(1);
}
else {
newNode.setPriority(2);
}
}
else {
newNode.setPriority(3);
}
if(isEmpty()) {
firstNode = newNode;
lastNode= newNode;
numberOfElements++;
}
else {
// if (newNode.getPriority()) {}
lastNode.setNext(newNode);
lastNode = newNode;
numberOfElements++;
}
}
As I said before I can call the both the methods in other classes but only in the line of code above does it not return the right values. When I test it in my main class every input comes out with priority 2 even though I can test the same words in the actual class and it returns the right values.
EDIT: So the problem is toString() converting the object to a string that says "hw4.WordPriorityQueue$Node@16f65612". What do I need to do to override toString() to say what I am putting into the object?
You need to override the toString()
method in your Node
class. Otherwise It will always return something like Node@4c203ea1
which is just the name of the class and its hashcode.
Also you can make your methods a little bit more elegant like this:
public static List<Character> vowels = Arrays.asList('a','e','i','o','u','A','E','I','O','U');
public static boolean startsWithVowel(String input) {
return vowels.contains(input.charAt(0));
}