java 使用 .equals() 方法将字符串与字符串数组进行比较

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5463874/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 11:13:53  来源:igfitidea点击:

comparing string to array of strings using .equals() method

javaarrays

提问by Sackling

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:

我正在尝试比较一个字符串数组(一个股票市场符号的 csv 文件,它被导入到一个数组列表中,然后转换为一个数组),但它似乎不起作用。我不是将它与正确的数据类型进行比较吗?这是我的代码:

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

任何帮助是极大的赞赏

回答by Bozho

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

首先,确保您的元素已正确填充。例如 - 没有任何空格(使用trim()

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

然后你可以使用更简单的东西:elementsList.contains(sym).

回答by Sanjay T. Sharma

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.

不要使用StringTokenizer; 而是使用String#split. 此外,请确保修剪您的值(用户输入和每个令牌)以解决虚假空格。在 Java 中,类名以大写字符开头。你永远不会关闭你的Reader,坏事。