jeudi 7 novembre 2019

Why does my while loop ignore certain conditional statements and run an infinite loop? [duplicate]

This question already has an answer here:

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'");
                }
            }
        }
    }
}

Running Test Image

Aucun commentaire:

Enregistrer un commentaire