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");
}
}
}