This question already has an answer here:
- How do I compare strings in Java? 23 answers
This is my JAVA code. I tried to make a code that detects "www" at the front in the string and terminates when "quit" input is received. However, the second if statement fails to detect "quit" and the program continues to run. What's the problem?
import java.util.Scanner;
public class URLcheck {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String url;
while(true) {
System.out.print("Please input String: ");
url=in.next();
if(url.length()<3) {
System.out.println(url+"is Too Short");
continue;
}
if(url=="quit") {
break;
}
else {
if (url.indexOf("www")!=0) {
System.out.println(url);
System.out.println("ERROR!: "+url+"is --NOT-- Start with 'www'");
}
else {
System.out.println(url+"is Start with 'www'");
}
}
}
}
}
Aucun commentaire:
Enregistrer un commentaire