Hi guys,
The written code gives the following output:
URL address: https://javarush.us, network protocol - https, domain extension - us
URL address: https://google.com, network protocol - https, domain extension - com
URL address: http://wikipedia.org, network protocol - http, domain extension - org
URL address: facebook.com, network protocol - unknown, domain extension - com
URL address: https://instagram, network protocol - https, domain extension - unknown
URL address: codegym.cc, network protocol - unknown, domain extension - unknown
Precisely in accordance with the task's conditions (IMHO)
The last piece of advice was like this:
Be sure that the checkDomainExtension(String) method returns the string "unknown" if the URL does not end with com, net, org, or us
Please, gimme a hint on how to fix it.
Thank you in advance!
package en.codegym.task.pro.task09.task0912;
/*
URL validation
*/
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Solution {
public static void main(String[] args) {
String[] urls = {"https://javarush.us", "https://google.com", "http://wikipedia.org", "facebook.com", "https://instagram", "codegym.cc"};
for (String url : urls) {
String protocol = checkProtocol(url);
String domainExtension = checkDomainExtension(url);
System.out.println("URL address: " + url + ", network protocol - " + protocol + ", domain extension - " + domainExtension);
}
}
public static String checkProtocol(String url) {
return isURL(url);
}
public static String checkDomainExtension(String url) {
return isDom(url);
}
public static String isURL(String a) {
String result = "";
String htps = "https";
String htp = "http:";
for (int i = 0; i < 5; i++) {
result += a.charAt(i);
}
if (result.equals(htps)) {
result = "https";
} else {
if (result.equals(htp)) {
result = "http";
} else {
result = "unknown";
}
}
return result;
}
public static String isDom(String a) {
String result = "";
for (int i = a.length() - 3; i < a.length(); i++) {
result += a.charAt(i);
}
if (result.contains(".") && result.length() <= 2 && result.contains("us")) {
return result.replace(".", "");
}
if (result.contains(".") && !result.contains("cc") || result.length() <= 4 && a.contains(".") && result.contains("net") || result.contains("com") || result.contains("org")) {
return result.replace(".", "");
}
return "unknown";
}
}