You are viewing a single comment's thread. Return to all comments →
import java.io.; import java.util.;
public class Solution {
public static void main(String[] args) { /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */ Scanner sc = new Scanner(System.in); while(sc.hasNext()){ String ip = sc.nextLine(); List<String> eachWord = new ArrayList<>(Arrays.asList(ip.split("\\."))); if(ip.charAt(ip.length()-1)=='.') eachWord.add(""); boolean check = true; if(eachWord.size()==4 && eachWord.get(0).length()>0){ for (String string : eachWord) { try { if(string.length() > 3){ check = false; break; } int num = Integer.parseInt(string); if (!(num >= 0 && num <= 255)) { check = false; break; } } catch (NumberFormatException e) { check = false; // Handles cases where parsing fails (e.g., empty strings or non-numeric parts) break; } } } else{ check = false; } if(check) System.out.println("true"); else System.out.println("false"); } }
}
Seems like cookies are disabled on this browser, please enable them to open this website
I agree to HackerRank's Terms of Service and Privacy Policy.
Java Regex
You are viewing a single comment's thread. Return to all comments →
import java.io.; import java.util.;
public class Solution {
}