-2
  • What's wrong with the searchStudent method that returns 0 every time? int searchStudent(String target)Finds and gives the desired index by name.

    import java.util.ArrayList;

class Student {

String name;
double gpa;

public Student(String name, double gpa) {
    this.name = name;
    this.gpa = gpa;
}

public String getName() {
    return name;
}

}

public class StackOverflow {

static ArrayList<Student> stu = new ArrayList<>();

public static void main(String[] args) {
    stu.add(new Student("Hadi", 2.3));  // <-- every time shows 'Hadi'
    stu.add(new Student("Jack", 1.8));
    stu.add(new Student("Sara", 4.6));

    System.out.println("----- Searching For A Student -----");
    int recurse = searchStudent("Jack");
    if (recurse == -1) {
        System.out.println("There is no Student with this name !!");
    } else {                       //if 'Jack' was in List show his name
        System.out.println(getStudent(recurse).getName());
    }
}

public static int searchStudent(String target) {
    int indexTarget = -1;
    for (Student z : stu) {
        if (z.getName() != null && z.getName().contains(target)) {
            indexTarget = z.getName().indexOf(target);
        }
    }
    return indexTarget;
}

public static Student getStudent(int index) {
    return stu.get(index);
}

}

Hadi
  • 3
  • 8
  • 3
    Because it finds a match at index zero? It must be that, given that it would return -1 if it didn't find anything. – Andy Turner Feb 03 '20 at 07:17
  • Does this answer your question? [How do I compare strings in Java?](https://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – Nicktar Feb 03 '20 at 07:20
  • Your target matches a name starting at the beginning of the String e.g. you are looking for *Fred* and the students name begins with *Fred* - this is a String indexOf not the index of the element. If you want the array index then loop through using an index – Scary Wombat Feb 03 '20 at 07:26
  • Other people answered pretty much to everything, but as a rule of thumb would be better to provide the test input of your run, and have you tried with different input? (I mean both the students array and the 'target' value) – Matteo Feb 03 '20 at 07:28

1 Answers1

0

tl;dr

Your searchStudent method is confusing (a) the index number returned by String::indexOf with (b) needing to keep a running tally of hits found.

I suggest you write out the search method’s algorithm in plain prose, then compare to your code.

Index 0 means match at front of string

The method String::indexOf returns an index (a zero-based position number) indicating the first position where your desired target string was found. For more about index-counting, see Wikipedia: Zero-based numbering.

When calling String::indexOf, a resulting index of zero (0) means your target is being found at beginning of the string under examination. For example, you are doing the equivalent of looking for Bob in the name Bobby (or in Bob for that matter).

String name = "Bobby" ;
String target = "Bob" ;
int index_Bob = name.indexOf( target ) ;
System.out.println( "index_Bob: " + index_Bob ) ;

index_Bob: 0

When calling String::indexOf, a resulting index of negative one (-1) means your target was not found, is not contained in the string.

String name = "Bobby" ;
String target = "Xyz" ;
int index_Xyz = name.indexOf( target ) ;
System.out.println( "index_Xyz: " + index_Xyz ) ;

index_Xyz: -1

Look for by in Bobby, and we get a result of 3 (the index for the 4th letter).

int index_by = name.indexOf( "by" ) ;
System.out.println( "index_by: " + index_by ) ;

index_by: 3

See this code run live at IdeOne.com.

Count

If you are trying to get a count of any "contains" test succeeding, and you don't really care where the target matched, just that it did match, then there is no need to call indexOf at all.

Change this:

        count = z.getName().indexOf(target);

… to this:

        count = ( count + 1 ) ;  // Increment the count of matches.

Stream

By the way, an advanced way to get the count is to use a Stream (see Tutorial). See my example code below.

I don't have your Student class, so I am just using String here. You would change it ( Student student ) -> student.getName().contains( "Bob" ) for your code.

    List< String > names = List.of(
        "Alice", "Bobby", "Bobette" , "Carol" 
    );

    long countBobs = 
        names
        .stream()
        .filter( 
            ( String name ) -> name.contains( "Bob" )
        )
        .count() 
    ;

See this code run live at IdeOne.com.

countBobs: 2

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
  • I checked your codes carefully but I didn't understand Could you please edit your code Simpler for beginner I'm a newbie from – Hadi Feb 12 '20 at 12:30