Get_ Maths
Get_ Maths

Reputation: 103

If-else working unexpectedly in android studio. why?

I am trying to make an app which takes URL and tells whether this URL is valid or not. (There is no any great intention for making this app but just to learn android development).

I used if-else in button. it must output "if" section but it gives out "else" section.

CODE

TextView tv;
EditText urlScan;
Button btn;
private static final String EXAMPLE_DOT_COM = "[a-z]+[\\.]{1}[a-z]{2,3}";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    tv = findViewById(R.id.tv);
    urlScan = findViewById(R.id.webInput);
    btn = findViewById(R.id.btn);


    String webUrl = urlScan.getText().toString();

    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (webUrl.equals(EXAMPLE_DOT_COM)){
                tv.setText("Valid URL");
            }
            else {
                tv.setText("Invalid");

            }
        }
    });

}

INPUT google.com

Expected output Valid URL

OUTPUT Invalid

Upvotes: 0

Views: 54

Answers (2)

Nitish
Nitish

Reputation: 3411

Don't need to use regex , there is a better option available , use URLUtil.isValidUrl(url) to check if it's a valid url or not

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    tv = findViewById(R.id.tv);
    urlScan = findViewById(R.id.webInput);
    btn = findViewById(R.id.btn);


    String webUrl = urlScan.getText().toString();

    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (URLUtil.isValidUrl(url)){
                tv.setText("Valid URL");
            }
            else {
                tv.setText("Invalid");    
            }
        }
    });

}

Upvotes: 1

Salih Kavaf
Salih Kavaf

Reputation: 1145

You're comparing a regex pattern to the URL you want to check, which isn't correct. Use regex matching instead:

import java.util.regex.Matcher; // Don't forget to add the required imports
import java.util.regex.Pattern;

TextView tv;
EditText urlScan;
Button btn;
private static final String EXAMPLE_DOT_COM = "[a-z]+[\\.]{1}[a-z]{2,3}";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    tv = findViewById(R.id.tv);
    urlScan = findViewById(R.id.webInput);
    btn = findViewById(R.id.btn);


    String webUrl = urlScan.getText().toString();

    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            
            // Create a new pattern...
            Pattern pattern = Pattern.compile(EXAMPLE_DOT_COM, Pattern.CASE_INSENSITIVE);

            // Match the URL string...
            Matcher matcher = pattern.matcher(webUrl);
            boolean matchFound = matcher.find();
            
            // Check whether it matches...
            if (matchFound){
                tv.setText("Valid URL");
            }
            else {
                tv.setText("Invalid");

            }
        }
    });

}

Your can learn more about Java Regex here

Upvotes: 1

Related Questions