Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6646813
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T00:27:48+00:00 2026-05-26T00:27:48+00:00

Hey guys I wrote this deleteNode() method that works if I used numbers(int) but

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T00:27:49+00:00Added an answer on May 26, 2026 at 12:27 am

    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:

    String delete = in.next();
    

    should be

    String delete = in.nextLine();
    

    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:

    • Add logging to your code to show value you were looking for, and each value as you tested it
    • Use a debugger to step through the code, watching variables
    • Use unit tests to test the code – those wouldn’t have shown you the problem (as it wasn’t in code you’d usually write tests for) but they would have given you greater confidence that the problem wasn’t in the tested code.

    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:

    if (curr.dataitems.equals(value))
    

    (but with careful null checking).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hey guys I'm not sure how to write this in AS3, but basically I
hey guys having this really simple problem but cant seem to figure out have
Hey guys I have a weird error I wrote this code against setbubblepopup $(document).ready(function
Hey guys, I have this quick bit of code that I can't figure out
Hey I guys I wrote a function to force out a json file, but
Hey guys.. Quick question: I wrote a simple JS that opens lightBox for image
Hey guys. I have a method that gets called each second which I want
Hey guys, PHP and MySQL newguy here. Wrote this php file which display the
hey guys , i know this is a stupid question but i hanged in
hey guys i have got this far with a chat system but now i

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.