0

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:

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<String> arrayList = new ArrayList<String>();

            //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 < 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 < 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

Any help is greatly appreciated

3
  • Since you're already using Scanner, why wouldn't you also use that for reading the file? Commented Mar 28, 2011 at 19:17
  • Not quite sure but the element array is an object array Object[].(could be a problem if you compare Object with String) why convert arrayList to array anyway better loop trough the arraList. for(String element:arrayList) Commented Mar 28, 2011 at 19:23
  • Note: Each call to nextToken() will advance to the next token. If you uncomment the println in the inner loop, your list will only be half as large and the values printed will not be in the list. Commented Mar 28, 2011 at 19:32

2 Answers 2

4

First, make sure your elements are filled properly. For example - without any whitespaces (use trim())

Then you can use something simpler: elementsList.contains(sym).

Sign up to request clarification or add additional context in comments.

2 Comments

yes contains works. There was already no white spaces so it shouldnt be a problem I don't think. The only thing is the symbols are capital letters so I will need to look up the method to make the user input capitals which I believe exists.
robert - when comparing, if you don't care about case sensitivity, you can use toLowerCase() for both. Or with your comparison code - equalsIgnoreCase(..)
2

Don't use StringTokenizer; instead use String#split. Also, make sure you trim your values (both user input and each token) to account for spurious whitespaces. In Java, class names start with upppercase characters. You never close your Reader, bad thing.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.