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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T10:04:48+00:00 2026-06-11T10:04:48+00:00

When comparing a string taken from console input to a string inside an array,

  • 0

When comparing a string taken from console input to a string inside an array, it is always false unless I add .toString(). Both strings are equal and it should work without adding the .toString(). Can anyone help me figure out why?

Here I get the string I want to compare from the console:

System.out.println("\nEnter the name you wish to remove from the list.");
String name = in.nextLine();
System.out.println("\n\"" + myNameList.remove(new MyOrderedList(name)) + "\"" + " removed from the name list\n");

Here is the remove method:

public T remove(T element) {
    T result;
    int index = find(element);

    if (index == NOT_FOUND) {
        throw new ElementNotFoundException("list");
    }

    result = list[index];
    rear--;

    /** shift the appropriate elements */
    for (int scan = index; scan < rear; scan++) {
        list[scan] = list[scan+1];
    }

    list[rear] = null;
    return result;
}

Here is the find method that is were the problem is:

private int find(T target) {
    int scan = 0, result = NOT_FOUND;
    boolean found = false;

    if (!isEmpty()) {
        while (!found && scan < rear) {
            if (target.equals(list[scan])) { // Not sure why this does not work until I add the .toString()s
                found = true;
            }
            else {
                scan++;
            }
        }
    }

    if (found) {
        result = scan;
    }
    return result;
}

The if (target.equals(list[scan])) always returns false unless I change it to if (target.toString().equals(list[scan].toString()).

I am using an ArrayList to represent an array implementation of a list. The front of the list is kept at array index 0. This class is extended to create a specific kind of list if that helps. I can post all classes if needed.

  • 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-11T10:04:49+00:00Added an answer on June 11, 2026 at 10:04 am

    If myNameList has a String generic parameter, then this will not work, because no String will equal a type of MyOrderedList.

    If myNameList has a MyOrderedList generic parameter, then you will need to make sure that you define an equals() method for it.

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

Sidebar

Related Questions

I'm comparing a string from the database to a list of strings in an
Dear all expert i had a problem for comparing my string from document.write with
After a few harsh lessons I now always use OrdinalIgnoreCase when comparing Strings in
Why when comparing a char against another it must be taken also from a
Hey I'm trying to take a String input from the user (in this case
yesterday(April 5th 2012) i'am trying comparing string which is in environment: computer 1 Java(TM)
When comparing a string literal with another string literal with the == operator (or
string a=I am comparing 2 string; string b=I am comparing 2 string; if(a==b) return
I need help with comparing a column to a string. There is a column
Possible Duplicate: Comparing Strings in Cocoa Why is this code not recognising the NSString

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.