0

Right, in this program I am supposed to be able to search for a person in the list. If I search for someone who is not in the list then the Found variable should be kept at false. If I search for someone who is in the list eg: "Ben" then Found should be set to true.

However for some reason, searching for someone who is in the list does not set found to true. It seems like the if statement which checks the player's input to the array is not working properly. I have no idea why this is. There are no errors. Can anyone help? Thanks

code:

    package com.test.main;

import java.util.Scanner;

    public class Main {
    public static void main(String[] args){
    String[] Names = new String[4];
    Names[0] = "Ben";
    Names[1] = "Thor";
    Names[2] = "Zoe";
    Names[3] = "Kate";

    int Max = 4;
    int Current = 1;
    boolean Found = false;

    System.out.println("What player are you looking for?");
    Scanner scanner = new Scanner(System.in);
    String PlayerName = scanner.nextLine();

    while(!Found && Current <= Max){
        //System.out.println(Names[Current-1]);
        //System.out.println("PLAYERNAME: " + PlayerName.length() + ", ARRAY: " + Names[Current-1].length());
        if(Names[Current-1] == PlayerName){
            //System.out.println("found");
            Found = true;
        }
        else{
            Current++;
        }
    }
    //System.out.println(Found);
    if(Found){
        System.out.println("Yes, they have a top score");
    }
    else{
        System.out.println("No, they do not have a top score");
    }
}
}
Subhrajyoti Majumder
  • 40,646
  • 13
  • 77
  • 103
  • 5
    Read [How do I compare strings in Java?](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – PakkuDon Feb 25 '14 at 05:05
  • Use String.equals method instead of == for comparing string values – Juned Ahsan Feb 25 '14 at 05:05
  • ...And you'll realize that `if(Names[Current-1] == PlayerName){` should be `if(Names[Current-1].equals(PlayerName)){`. – Rahul Feb 25 '14 at 05:06

4 Answers4

1

String is Object and object equality checks with equals method.

== operator is used for object reference equality(means two reference is pointing to the same object or not!) or primitive(int, double, ...) equality.

if(Names[Current-1] == PlayerName)

should be

if(Names[Current-1].equals(PlayerName))

In this case there might be chance of getting NullPointerExceotion if Names[Current-1] is null. To avoid this situation java 7 provides a static Utility class java.util.Objects.

This class consists of static utility methods for operating on objects. These utilities include null-safe or null-tolerant methods for computing the hash code of an object, returning a string for an object, and comparing two objects.

Documentation

so the best way would be -

if(java.util.Objects.equals(Names[Current-1],PlayerName))
Subhrajyoti Majumder
  • 40,646
  • 13
  • 77
  • 103
0

you are using String object which compare reference instead of value.

try:

if (PlayerName.equals(Names[Current-1])) {
    ...
}
Zaheer Ahmed
  • 28,160
  • 11
  • 74
  • 110
0

Use Names[Current-1].equals(PlayerName) instead of Names[Current-1] == PlayerName

cerberus
  • 531
  • 4
  • 14
0

I don't know whether you have knowledge on Collections but the following code would simplify your efforts. And the reason for the failure of above code is that you've used "==" instead of "equals". And for you information kid, always have a habit of using variables names starts with small letter eg. playerName

    public static void main(String[] args) {

    List<String> names = new ArrayList<String>();
    names.add("Ben");
    names.add("Thor");
    names.add("Zoe");
    names.add("Kate");

    boolean found = false;

    System.out.println("What player are you looking for?");
    Scanner scanner = new Scanner(System.in);
    String playerName = scanner.nextLine();

    for (String name : names) {
        if (playerName.equalsIgnoreCase(name))
            found = true;
    }
    // System.out.println(Found);
    if (found) {
        System.out.println("Yes, they have a top score");
    } else {
        System.out.println("No, they do not have a top score");
    }
}