0
import java.util.ArrayList;
import java.util.Scanner;

public class JavaApplication10Arraylistandobjects {

    static Scanner user_input = new Scanner(System.in);

    public static void main(String[] args) {
        test();
    }

    public static void test(){
        ArrayList<mainclass> me = new ArrayList <> ();
        mainclass ob;
        for (int i=0;i<2;i++){
            ob = new mainclass();
            System.out.println("name");
            ob.name = user_input.nextLine();
            System.out.println("sname");
            ob.sname = user_input.nextLine();
            me.add(ob);
        }
        System.out.println("Show List: " + me);
        System.out.println("Confirm if is true or false: " + me.get(1).toString().contains("max"));
        System.out.println("what is index of: " + me.get(1).toString().indexOf("max"));
    }
}

public class mainclass {

    public String getName() {
    return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSname() {
        return sname;
    }

    public void setSname(String sname) {
        this.sname = sname;
    }

    @Override
    public String toString() {
        return "mainclass{" + "name=" + name + ", sname=" + sname + '}';
    }

    String name;
    String sname;
}

My questions is how can I find correctly indexOf string. For example when I am checking if string "max" exist - it shows me "true" and when I am trying to find index of string "max" it shows me index 15 which is not correct.

P.S. I found an article with the same problem where it says that I have to override equals and hashcode - I've done it but anyway I got the same problem.

Please point me to the right direction. What I am doing wrong here, can someone explain me pls.

These are my inputs and output.

  • run: name Jony sname Bravo

name max sname hitman

Show List:[mainclass{name=Jony, sname=Bravo}, mainclass{name=max, sname=hitman}]

Confirm if is true or false: true what is index of: 15

BUILD SUCCESSFUL (total time: 11 seconds)

14
  • Why do you think 15 is not correct? Commented Mar 30, 2018 at 20:14
  • And what would you expect the correct value to be? Commented Mar 30, 2018 at 20:14
  • 1
    As I understand I don't need index from mainclass but I need index from my ArrayList Commented Mar 30, 2018 at 20:18
  • 2
    You seem to be confusing two indexOf methods (methods of String and List). Commented Mar 30, 2018 at 20:21
  • 1
    It's not correct, but it's getting closer. I think we can at least see what the issue is and what you want to be doing, now, and can write an answer. Commented Mar 30, 2018 at 20:26

1 Answer 1

1

The line:

    System.out.println("what is index of: " + me.get(1).toString().indexOf("max"));

has a problem, in that you're getting the object in the me list at index 1, getting its toString(), and looking for "max" in there. What you actually want to do, as I understand it, is look through the me list and find the place in the list with "max" in it.

P.S. I found an article with the same problem where it says that I have to override equals and hashcode - I've done it but anyway I got the same problem.

If you did that, it would allow you to do something like this:

x = new mainclass();
x.setName("Max");
System.out.println("what is index of: " + me.indexOf(x));

However, there's still a potential problem. Unless you set your equals() and hashCode() to only look at the name and not also sname, then it's not going to find anything unless the sname also matches.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.