This is my first post, so I hope this is an alright way to ask a question.
I'm working on a project where I'm trying to check if a name that I input via command line is in an array of names. The array is made up of names from a text file and it looks like that runs properly but when I try to verify if the name that I typed in is in the list already, it always comes back false. Really not sure what could be causing this, I tried changing the array to an arrayList and as a HashSet but still am having the same problem, not really sure where to go from here.
Here's my driver:
public static void main(String[] args){
String fileName = "Company_list.txt";
Scanner kybd = new Scanner(System.in);
Scanner inputStream = null;
Checker ch = new Checker();
try {
inputStream = new Scanner(new File(fileName));
} catch(FileNotFoundException e) {
System.out.println("Error opening the file " + fileName);
System.exit(0);
}
while (inputStream.hasNextLine()) {
ch.tokens.add(inputStream.nextLine());
}
ch.tokenArray = ch.tokens.toArray(new String[0]);
//System.out.println(Arrays.asList(ch.tokenArray));
System.out.println("Enter a company name:");
ch.check(kybd.next());
inputStream.close();
}
And here is my class with the array. I commented out a do while loop I used originally and left in a for loop that isn't working correctly either:
import java.util.*;
public class Checker {
List<String> tokens = new ArrayList<String>();
String[] tokenArray;
Driver dr = new Driver();
public void check(String a1){
int N = tokenArray.length;
for(int i = 0; i < N; i++){
if(tokenArray[i] == a1){
System.out.println("Already a client, try another.");
}else
System.out.println("Potential new client.");
System.out.println(a1);
System.exit(0);
}
//do{
//if(Arrays.asList(tokenArray).contains(a1)){
//System.out.println("Already a client, try another.");
//}else
//System.out.println("Potential new client.");
//System.exit(0);
//}while(Arrays.asList(tokenArray).contains(a1) == true);
}
}
Thanks in advance for taking a look.