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 7792139
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T22:06:00+00:00 2026-06-01T22:06:00+00:00

I am just learning Java and have created my own Word List and Word

  • 0

I am just learning Java and have created my own Word List and Word Node classes in Java. However, I have found that my WordList Index Of method always returns -1. Can someone please tell me what is wrong with it? Has it something to do with using Scanner? (My textbook says it is not my add method). My code is below (each class in its own file):

WordNode.java:

public class WordNode {

private String value;
private WordNode next;

public WordNode(String newValue) {
    value = newValue;
    next = null;
}

public WordNode(String newValue, WordNode nextNode) {
    value = newValue;
    next = nextNode;
}

public String getValue() {
    return value;
}

public WordNode getNextNode() {
    return next;
}

public void setNextNode(WordNode node) {
    next = node;
}}

WordList.java:

public class WordList {
private WordNode first;

public WordList(String firstNode) {
    first = new WordNode(firstNode);
}

public void add(String newValue) {
    first = new WordNode(newValue, first);
}

public void remove(String oldValue) {
    if (first.getValue() == oldValue) {
        first = first.getNextNode();
        return;
    }
    WordNode temp = first;
    while (temp != null && temp.getNextNode().getValue() != oldValue)
        temp = temp.getNextNode();
    if (temp != null)
        temp.setNextNode(temp.getNextNode().getNextNode());
}

public void moveToStart(String toMove) {
    remove(toMove);
    add(toMove);
}

public int indexOf(String item) {
    WordNode temp = first;
    int i = 1;
    while (temp != null && temp.getValue() != item) {
        temp = temp.getNextNode();
        i++;
    }
    if (temp == null)
        return -1;
    else
        return i;
}

public String itemAtIndex(int index) {
    WordNode temp = first;
    int i = 1;
    while (temp != null && i != index) {
        temp = temp.getNextNode();
        i++;
    }
    if (i == index)
        return temp.getValue();
    else
        return "";
}}

MTFencoder.java:

import java.io.FileReader;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.util.Scanner;

public class MTFencoder {

public static void main(String[] args) {
    try
    {
        Scanner scan = new Scanner(new FileReader(args[0]));
        WordList dictionary = new WordList(scan.next());

        while (scan.hasNext()) {
            String newWord = scan.next();
            int index = dictionary.indexOf(newWord);
            if (index == -1) {
                System.out.println("0 " + newWord);
                dictionary.add(newWord);
            }
            else {
                System.out.println(Integer.toString(index));
                dictionary.moveToStart(newWord);
            }
        }
    }
    catch (Exception ex) {
        System.out.println("An error occured while reading the file. Check that it exists and is valid.");
    }
}}

Thanks in advance.
Daniel

  • 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-01T22:06:01+00:00Added an answer on June 1, 2026 at 10:06 pm

    In Java, to check equality of objects, including Strings, you have to use .equals method. So in indexOf and remove, you will have to use first.getValue().equals(oldValue) or similar.

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

Sidebar

Related Questions

I am learning Java and just found that the Interface can have fields, which
I have just began learning Java programming, I have created a simple game using
I'm just learning Java... I have 2 custom classes. One is a Fraction and
I'm just learning java and following a book. I have a program written via
I have just started learning java and i have about 8 months time. My
I'm just learning Java, and I find that the Java docs/books put a strong
I'm learning java and now I have this question. I created a class named
I have just started learning java, and know only a small amount of code,
I'm just beginning to learn Java and I have been very frustrated about learning
just learning Java and I know this may sound stupid but I have to

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.