0

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.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
Hexagone
  • 1
  • 2
  • 2
    You are comparing strings with `==` instead of `equals`. – user1803551 Feb 04 '16 at 01:33
  • Your commented-out code works fine, btw. – Kenney Feb 04 '16 at 01:36
  • Also your code after the else is not in curly braces. Only the first line -- `System.out.println("Potential new client.");` is associated with the else clause. – Hovercraft Full Of Eels Feb 04 '16 at 01:37
  • Thanks, yea it was the `==` instead of `equals`. – Hexagone Feb 04 '16 at 02:48
  • check()'s for loop also has a problem: your logic only examines the first entry in tokenarray, and declares "hit" or "miss". Loop should continue to allow all tokenarray elements to be compared. If you modify check() to return boolean (true=found, false=not-fond) then something like this: for(...) if( tokenarray[i].equals(a) ) { return true; } /* end of if*/ } /* end of for-loop */ return false; /* not found */ } /* end of check() method */ – jgreve Feb 04 '16 at 17:24

0 Answers0