Hey guys I wrote this deleteNode() method that works if I used numbers(int) but doesn’t when I try to pass a string in. I’m printing out a String[] list of names and I’m trying to delete a certain name off the list. When I enter a name, it prints “Node not found”. Like I said, if I print out a list of numbers it works great but if I change up and print a string it doesnt. Any help is appreciated.
public class BigNode {
public String dataitems;
public BigNode next;
BigNode front ;
public void initList(){
front = null;
}
public BigNode makeNode(String number){
BigNode newNode;
newNode = new BigNode();
newNode.dataitems = number;
newNode.next = null;
return newNode;
}
public boolean isListEmpty(BigNode front){
boolean balance;
if (front == null){
balance = true;
}
else {
balance = false;
}
return balance;
}
public BigNode findTail(BigNode front) {
BigNode current;
current = front;
while(current.next != null){
//System.out.print(current.dataitems);
current = current.next;
} //System.out.println(current.dataitems);
return current;
}
public void addNode(BigNode front ,String number){
BigNode tail;
if(isListEmpty(front)){
this.front = makeNode(number);
}
else {
tail = findTail(front);
tail.next = makeNode(number);
}
}
public void deleteNode(BigNode front, String value) {
BigNode curr, previous = null; boolean found;
if (!isListEmpty(front)){
curr = front;
found = false;
while ((curr.next != null) && (!found)) {
if(curr.dataitems.equals(value)) {
found = true;
}
else {
previous = curr;
curr = curr.next;
}
}
if (!found) {
if(curr.dataitems.equals(value)) {
found = true;
}
}
if (found) {
if (curr.dataitems.equals(front.dataitems)){ // front.dataitems may be wrong .dataitems
front = curr.next;
} else {
previous.next = curr.next;
}
} else {
System.out.println("Node not found!");
//curr.next = null; // Not sure If this is needed
}
}
showList(front);
}
public void printNodes(String[] len){
int j;
for (j = 0; j < len.length; j++){
addNode(front, len[j]);
} showList(front);
}
public void showList(BigNode front){
BigNode current;
current = front;
while ( current.next != null){
System.out.print(current.dataitems + ", ");
current = current.next;
}
System.out.println(current.dataitems);
}
public static void main(String[] args) {
String[] names = {"Billy Joe", "Sally Mae", "Joe Blow", "Tasha Blue"};
BigNode x = new BigNode();
x.printNodes(names);
Scanner in = new Scanner(System.in);
String delete = in.next();
x.deleteNode(x.front, delete);
}
String[] names = {name1, name2, name3, name4}
-First it prints the list, then ask what name to delete.
EDIT: Okay, I’ve found out what’s wrong with the sample code you’ve posted.
You’re calling
Scanner.next()which reads a single word. All of your node values are two words. So if I type in “Sally Mae” it’s actually just looking for “Sally”.This has nothing to do with the majority of the code in
BigNode(although that could certainly be made more elegant). Basically this:should be
Now I would strongly suggest that you don’t just change the code, but instead think about the ways you could have diagnosed this for yourself:
If you try some or preferrably all of those approaches, you’ll have learned a lot more from this question than just how to use
Scanner…In various places, you’re comparing string references by using the == operator. That will only find your node if you pass in a reference to one of the actual string objects which exists in the list – not a reference to an equal string object.
You want something like:
(but with careful
nullchecking).