Note that there are some explanatory texts on larger screens.

plurals
  1. POcomparing string to array of strings using .equals() method
    text
    copied!<p>I am trying to compare an array of strings (a csv file of stock market symbols that was imported into an arraylist and then converted to an array) but it does not seem to be working. Am I not comparing it to the correct data type or something? Here is my code:</p> <pre><code>import java.io.BufferedReader; import java.io.FileReader; import java.util.Scanner; import java.util.StringTokenizer; import java.util.ArrayList; public class search { public static void main(String[] args) { try { //csv file containing data String strFile = "companylist.csv"; //create BufferedReader to read csv file BufferedReader br = new BufferedReader(new FileReader(strFile)); String strLine = ""; StringTokenizer st = null; int lineNumber = 0, tokenNumber = 0; //create arraylist ArrayList&lt;String&gt; arrayList = new ArrayList&lt;String&gt;(); //read comma separated file line by line while ((strLine = br.readLine()) != null) { lineNumber++; //break comma separated line using "," st = new StringTokenizer(strLine, ","); while (st.hasMoreTokens()) { //display csv values tokenNumber++; arrayList.add(st.nextToken()); //System.out.println("Line # " + lineNumber + ": "+ st.nextToken() + " " + st.nextToken()); } //end small while //reset token number tokenNumber = 0; } //end big while loop //send csv to an array Object[] elements = arrayList.toArray(); /* for(int i=0; i &lt; elements.length ; i++) { System.out.println(elements[i]); } */ Scanner input = new Scanner(System.in); System.out.print("Enter Ticker symbol"); String sym = input.next(); for (int i = 0; i &lt; elements.length; i++) { if (elements[i].equals(sym)) { System.out.println("match"); } } } catch (Exception e) { System.out.println("Exception while reading csv file: " + e); } }//end main }//end class </code></pre> <p>Any help is greatly appreciated</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload