Saurav Gharti Magar
Saurav Gharti Magar

Reputation: 174

String read from the csv file not equal to String defined code in java

I have a program to read data from the csv file and manipulate the data. I read the data from the csv file using the BufferedReader readLine() method and split the line read with "," which gave me the array of String. Then, I add the trimmed element of the array to object. When I get the data from the object and compare with the same String, It shows that String are different.

public class Main {
    public static void main(String[] args){
        GeneralHospitalDataImpl hospitalData = new GeneralHospitalDataImpl();
        File file = new File("Hospital General Information.csv");
        List<Hospital> data = hospitalData.getHospitalData(file);
        int i=0;
        for(Hospital hospital: data){
            String a = hospital.getState();
            System.out.println(a);
            System.out.println(a.equals("AL"));
            if (a.equals("AL"))
                System.out.println(hospital.getState());
            i++;
            if(i==5)
                break;
        }

    }
}

Here is my output:

"AL"
false
"AL"
false
"AL"
false
"AL"
false
"AL"
false

Upvotes: 0

Views: 834

Answers (1)

glw
glw

Reputation: 1664

Problem is with quotation marks. You are comparing "AL" and "\"AL\"" and these strings aren't equal.

Upvotes: 3

Related Questions