We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
class MyRegex {
public static boolean checkIP (String input) {
int startNum = 0 ;
for (int i=1 ; i < 5 ; i++){
int currentDot = input.indexOf(".", startNum);
if (currentDot == -1 && i != 4){return false ;}
int currentNum ;
try {
if (currentDot == -1) {currentDot = input.length();}
currentNum = (int) Integer.parseInt(input.substring(startNum, currentDot));
if (currentNum > 255) {return false;}
if (input.substring(startNum, currentDot).length() > 3 || input.substring(startNum, currentDot).length() == 0) {return false;}
if (input.contains(" ")){return false;}
}
catch (Exception e){
return false ;
}
startNum = currentDot + 1;
if (i == 3 && input.indexOf(".", startNum) > 0){return false ;}
}
return true;
}
}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Java Regex
You are viewing a single comment's thread. Return to all comments →
here is my code without regex in java
import java.io.; import java.util.;
public class Solution {
String input = reader.readLine(); while (input != null){ System.out.println(MyRegex.checkIP (input)); input = reader.readLine(); } }
}
class MyRegex { public static boolean checkIP (String input) { int startNum = 0 ; for (int i=1 ; i < 5 ; i++){ int currentDot = input.indexOf(".", startNum);
} return true; }
}