This question already has an answer here:
- How do I compare strings in Java? 23 answers
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");
}
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