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

  • Home
  • SEARCH
  • 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 9003481
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T00:43:04+00:00 2026-06-16T00:43:04+00:00

I thought this would be very easy but I am having problems searching nodes

  • 0

I thought this would be very easy but I am having problems searching nodes from a text file.

The data from the text file follows:

1

2

3

4

5

The data is being stored in the String “word”. They are numbers to avoid complexity.

Issue is- is that it keeps on returning false when calling the search method.

public class Search
    {

        static int count;  // number of elements
        Search ()
    {
        count = 0;
    }


    static void inputdata (Node head, Node node) throws IOException
    {
        BufferedReader stdin = new BufferedReader (new InputStreamReader (System.in));
        BufferedReader reader = new BufferedReader (new FileReader ("Words.txt"));

        String word;
        String line = null;


        while ((line = reader.readLine ()) != null)
        {
            word = (line);
            node = new Node (word);
            node.next = head;
            head = node; // need to set the new head of the list to the node that was just inserted
            count++;
        }


        reader.close ();

        node = head;
        System.out.println ("Here is the list:");
        System.out.println ();
        do
        {
            System.out.println (node.data);
            node = node.next;
        }
        while (node != null);
        System.out.println ();
        System.out.println ();
    }




    static boolean Found (String search, Node head, Node node)  // recursive search method
    {
        boolean found;

        Node temp; // sets a termpoary node to the head
        node = head;
        temp = head;

        while (temp != null)
        {
            if (temp.data.equals (search))
                return true;
            temp = temp.next;

        }
        return false;
    }


    public static void main (String str[]) throws IOException
    {
        BufferedReader stdin = new BufferedReader (new InputStreamReader (System.in));
        DecimalFormat df = new DecimalFormat ("#");

        //Search list = new Search (); //calls list via constructor
        Node head = null;
        Node node = null;

        inputdata (head, node);



        System.out.println (count + " entries");

        String search, repeat;


        System.out.println ();
        System.out.println ("Which word do you want to search within the linked list?"); // returns true/false from a method
        search = stdin.readLine ();

        System.out.println (Found (search, head, node));

    }
}

other file (class):

public class Node
{
    Node next, prev;
    String data;

    public Node (String data)
    {
        this.data = data;
    }
}
  • 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-06-16T00:43:04+00:00Added an answer on June 16, 2026 at 12:43 am

    Your problem is that your linked list only ever has one object in it at a time, since you are always setting node.next to head, and head to node. Therefore, you will always only print the same number (which I am going to guess, is exactly what you’re seeing).

    What you need to do is to fix your insertion routine. Something like this should do (mind you that it’s untested)

    Node prev;
    
    while((line = reader.readLine()) != null) 
    {
        word = line;
        node = new Node(word);
        node.next = null;
    
        if(head == null) {
            head = node;
        } 
    
        if(prev != null) {
            prev.next = node;
        }
        prev = node;
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I thought this would be very easy. I downloaded the JDK, extracted src.zip, modified
I thought this would be fairly easy, but I'm totally baffled. I want one
I thought this would be pretty easy but I'm running into all sorts of
I thought this would be a very common thing, but I couldn't find how
I thought this would be easy to find, but a google search has been
I thought this would be fairly simple but it turns out not to work
I thought this would be easy. The Google makes me think otherwise. What I
Okay at first I thought this would be pretty straightforward. But I can't think
OK so I thought this would be pretty simple but it's tripping me up..
Ok, I thought this would be simpler but I was probably wrong.. My parser

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.