always return false when compare an existing string from a test file to the newly enterer object with parameter

James Richard Lee

This is my method for checking if the first three elements are the same

carModel is a string

purchasePrice is a double

purchaseDate is a Date

I stored these in a txt file, separating with a comma and a space like ", "

However, this method always returns false,even if i input just the same record. But when I changed the first"&&" into "||", it would return true.

public static boolean exist(Car c) {
    String line = "";
    File file = new File("Car Records.txt");
    try {
        BufferedReader fr = new BufferedReader(new FileReader(file));
        while ((line = fr.readLine()) != null) {
            String[] s = line.split(", ");
            if ((s[0].equals(c.carModel)) &&(s[1].equals(c.purchasePrice))&& (s[2].equals(c.purchaseDate))
                    && (s[5].equals(false)) ){
                return true;
            }
        }
        fr.close();
    } catch (IOException e) {
        System.out.println("Error reading file");
    }
    return false;

}
Eran

s[0],s[1] and s[2] are Strings, so comparing them to double or to Date will never return true.

You'll have to parse the input Strings into the correct type before making the comparison.

You'll need something like this (I didn't fix the Date comparison, since I don't know how you represent the date in your input file):

        if ((s[0].equals(c.carModel)) &&(Double.parseDouble(s[1])==c.purchasePrice)&& (s[2].equals(c.purchaseDate))
                && (s[5].equals("false")) ){
            return true;
        }

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Always return false when comparing an object that uses parameter

File.Exists(Path) always return false when get 'string (Path)' from text file

when i enter a String by input from user and compare it with other string , it always false, explained below ,WHY?

Cypress-compare file contents with an array always return false

Bash return value of test -e $file always returns false

bcrypt.compare with Promise always return false

Compare password in WordPress always return false

Python - Compare number with boolean (always return false)

Why can't i compare this two string ? The string input is the same, but it always return false

jQuery compare object always resulting false

How to compare new string name with existing from txt file?

Compare jquery .data() with String always false

Why does the -e file existence test always return false for a filename containing non-ASCII characters?

Comparison of two String return always false java

File.Exist always return false

Always return false when i authenticate an user

bcrypt-nodejs compare function always return false

Why won't TS say "This condition will always return 'false'" when comparing a string to a null?

Why always return "None" string, like my if statment was always false?

Why would Test-AzureName always return false?

TestCafe test script checkbox.checked always return false even when checked, how can I check the checkbox state in an if-condition?

String comparison from nested for returns always false

This condition will always return 'false' since the types 'string' and '() => string' have no overlap

python - comparing a newly written file with filecmp.cmp() always returns False?

regex.test() keep returning false when test with string from FileSystem.readAsStringAsync

.Equals or == return false, always

getCheckedItemPositions always return false

SQL to always return false

How to test if Callable parameter will return a string with Reflection?