dimanche 24 mai 2015

Strings don't match even though they are suppossed to [duplicate]

This question already has an answer here:

I have a method that gets the extension of a file:

    public String getExtension(File file){
        String name = file.getName();

        try{
            String ext = name.substring(name.lastIndexOf("."));
            return ext;
        }catch(Exception e){
            return "";
        }
    }

When I check if the extension is equal to a string, it says it isn't even though it is:

    if(extens == ".bruh"){
        System.out.println("Ends with bruh!");
    }else{
        System.out.println("Doesn't end with bruh");
    }

enter image description here

Here is all the code:

    public getExt(){
        File file = new File("C:/Java/test.bruh");
        String extens = getExtension(file);
        check(extens);
    }

    public String getExtension(File file){
        String name = file.getName();

        try{
            String ext = name.substring(name.lastIndexOf("."));
            return ext;
        }catch(Exception e) {
            return "";
        }
    }

    public void check(String extens){
        if(extens == ".bruh"){
            System.out.println("Ends with bruh!");
        }else{
            System.out.println("Doesn't end with bruh");
        }
    }

Thanks in advance!

Aucun commentaire:

Enregistrer un commentaire