Is there any way to count integer elements in text file? - java

So I have a text file as :
and I want to count the number of integers in the first row.
// e.g. The first row : 3 12 1 8 5 8 1 2 1 4 --> 10
Can I do that with a stream or for statement or another way?
I tried with for and it didn't work for me and I couldn't find any useful solution. Please, help me.
public class Egyszamjatek {
public static void main(String[] args) throws IOException {
List<String> game = Files.readAllLines(Paths.get("egyszamjatek.txt"));
ArrayList<OneGame> games = new ArrayList<>();
for (String game1 : game) {
String[] split = game1.split(" ");
int rounds = Integer.parseInt(split[0]) + Integer.parseInt(split[1]) + Integer.parseInt(split[2])
+ Integer.parseInt(split[3]) + Integer.parseInt(split[4]) + Integer.parseInt(split[5])
+ Integer.parseInt(split[6]) + Integer.parseInt(split[7]) + Integer.parseInt(split[8])
+ Integer.parseInt(split[9]);
String names = split[10];
games.add(new OneGame(rounds, names));
}
System.out.println("3.feladat: number of players : " + game.stream().count());
System.out.println("4. feladat: number of rounds: " );
}
static class OneGame {
int rounds;
String names;
public OneGame(int rounds, String names) {
this.rounds = rounds;
this.names = names;
}
}
}

solution with for loop
String firstLine = "3 12 1 8 5 8 1 2 1 4";
String[] splits = firstLine.split(" ");
int count = 0 ;
for(String intStr:splits){
try {
int i = Integer.parseInt(intStr);
count++;
}catch (NumberFormatException e){
e.printStackTrace();
}
}
System.out.println(count);

You could do something like:
List<OneGame> games = Files.lines(Paths.get("egyszamjatek.txt")) // Stream<String> each line as a single String
.map(g -> {
String[] split = g.split(" ");
int rounds = (int) Arrays.stream(split)
.filter(a -> isInteger(a)) // filter only integers
.count(); // count how many integers e.g. 10 in your first line
return new OneGame(rounds, split[rounds]); // create an instance with count and name
}).collect(Collectors.toList()); // collect to list
where isInteger(a) is a util that you can use from this answer. Its implementation would be :
public static boolean isInteger(String str) {
if (str == null) {
return false;
}
if (str.isEmpty()) {
return false;
}
int i = 0;
if (str.charAt(0) == '-') {
if (str.length() == 1) {
return false;
}
i = 1;
}
for (; i < str.length(); i++) {
char c = str.charAt(i);
if (c < '0' || c > '9') {
return false;
}
}
return true;
}
Note: the code relies on certain assumptions for e.g. the integer values for the number of rounds would supersede the name of the game and hence uses split[rounds] to access the name.

Related

Append string after nth occurrence of a string

I have a string s to which I want to append another string s1 at the specified position.
String s = "17.4755,2.0585,23.6489,12.0045";
String s1=",,,,"
Now I want to add the string s1 after the n-th occurrence of "," character.
I have just started learning Java.
You can use the following method:
public String insert(int n, String original, String other) {
int index = original.indexOf(',');
while(--n > 0 && index != -1) {
index = original.indexOf(',', index + 1);
}
if(index == -1) {
return original;
} else {
return original.substring(0, index) + other + original.substring(index);
}
}
Working with Strings directly is not worth the trouble.
One easy way would be to turn your String into a List and manipulate that.
public void test() {
String s = "17.4755,2.0585,23.6489,12.0045";
// Split s into parts.
String[] parts = s.split(",");
// Convert it to a list so we can insert.
List<String> list = new ArrayList<>(Arrays.asList(parts));
// Inset 3 blank fields at position 2.
for (int i = 0; i < 3; i++) {
list.add(2,"");
}
// Create my new string.
String changed = list.stream().collect(Collectors.joining(","));
System.out.println(changed);
}
Prints:
17.4755,2.0585,,,,23.6489,12.0045
I think this is what you want
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String s = "17.4755,2.0585,23.6489,12.0045";
String s1=",,,,";
System.out.println("Enter Nth Occurrence");
try {
int n = scanner.nextInt();
long totalOccurrence = 0;
if (n != 0) {
totalOccurrence = s.chars().filter(num -> num == ',').count();
if (totalOccurrence < n) {
System.out.println("String s have only " + totalOccurrence + " symbol \",\"");
} else {
int count = 0;
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) == ',') {
count++;
if (count == n) {
String resultString = s.substring(0, i) + s1 + s.substring(i, s.length());
System.out.println(resultString);
}
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
System.out.println("Wrong input");
}
}
}
Output :
1. Enter Nth Occurrence
5
String s have only 3 symbol ","
2. Enter Nth Occurrence
2
17.4755,2.0585,,,,,23.6489,12.0045

Handling an Array in Java (two columns)

For instance suppose I have the following String
String S = "5,a\n" +
"6,b\n" +
"9,a";
The format is always the same - one digit, then comma, then one character and then line end character.
For looping each row in String I use
for(String a : S.split("\\n")){}
I want to learn the character with highest amount, when grouped by character. For Instance, there is only one "b", so value is 6; whereas "a" has two lines, so its value is 5 + 9 = 14. Since 14 is maximum here, I want to find out "a" and 14 and save them in variables.
You can do something like below :
public static void main (String[] args) throws java.lang.Exception
{
String S = "5,a\n" +
"6,b\n" +
"9,a";
String[] lines = S.split("\\n");
Map<String, Integer> map = new HashMap<String, Integer>();
for( String t : lines )
{
String[] e = t.split(",");
Integer digit = Integer.parseInt(e[0]);
String c = e[1];
if ( map.get(c) != null )
{
Integer val = map.get(c);
val += digit;
map.put( c, val );
}
else
{
map.put( c, digit );
}
}
int max = 0;
String maxKey = null;
for ( String k : map.keySet() )
{
if ( map.get(k) > max )
{
max = map.get(k);
maxKey = k;
}
}
System.out.println("The maximum key is : " + maxKey );
System.out.println("The maximum value is : " + max );
}
Output is :
The maximum key is : a
The maximum value is : 14
Use a HashMap to store each pair, with the letter as the key. If the entry doesn't exist, put the first number. If it exists, get the entry and add the number, and then put the sum.
import java.util.HashMap;
import java.util.Map;
public class ParseTest {
public static void main(String[] args) {
String S = "5,a\n" + "6,b\n" + "9,a";
String maxKey = null;
int maxVal = 0;
Map<String, Integer> sums = new HashMap<>();
for (String a : S.split("\\n")) {
String[] split = a.split(",");
int value = Integer.parseInt(split[0]);
String key = split[1];
if (sums.containsKey(key)) {
sums.put(key, sums.get(key) + value);
} else {
sums.put(key, value);
}
if (sums.get(key) > maxVal) {
maxVal = sums.get(key);
maxKey = key;
}
}
System.out.println("Max key: " + maxKey + ", Sum: " + maxVal);
}
}
After finishing my answer, I found that many similar answers have been posted out :). Anyway, my solution:
public static void main(String[] args) {
String S = "5,a\n6,b\n9,a";
Map<String, Integer> map = new HashMap<String, Integer>();
String highestAmountChar = "";
int highestAmount = 0;
for (String str : S.split("\\n")) {
String[] amountChar = str.split(",");
if (map.get(amountChar[1]) == null) {
map.put(amountChar[1], Integer.parseInt(amountChar[0]));
} else {
map.put(amountChar[1], map.get(amountChar[1]) + Integer.parseInt(amountChar[0]));
}
if (highestAmount < map.get(amountChar[1])) {
highestAmount = map.get(amountChar[1]);
highestAmountChar = amountChar[1];
}
}
System.out.println("The character " + highestAmountChar + " has highest amount " + highestAmount);
}
You could use something like this without using HashMap or any collection for that matter
import java.util.Arrays;
public class Test {
public static void main(String args[]) {
String S = "5,a\n" +
"6,b\n" +
"9,a";
// Separate the string by number and letter
String[] separated = S.split("\\n");
// Create a new array to store the letters only
char[] letters = new char[separated.length];
// Write the letter
for (int i = 0; i < letters.length; i++) {
letters[i] = separated[i].charAt(2);
}
// Sort them haha
Arrays.sort(letters);
// And now find out which letter is repeated most
// Store the first letter
char previous = letters[0];
// Make it the most repeated one for now
char mostRepeated = letters[0];
int count = 1;
int maxCount = 1;
for (int i = 1; i < letters.length; i++) {
// since the array is sorted if the actual letter is the same as the previous one then keep counting
if (letters[i] == previous)
count++;
else {
if (count > maxCount) {
mostRepeated = letters[i - 1];
maxCount = count;
}
previous = letters[i];
count = 1;
}
}
char answer = count > maxCount ? letters[letters.length-1] : mostRepeated;
// Once you get the letter now just add all the numbers that goes with it
int sum = 0;
for (String s:separated) {
if (s.charAt(2) == answer) {
sum += Character.getNumericValue(s.charAt(0));
}
}
// Print the result by printing the letter and it sum
}
}

Java: Find the longest sequential same character array

I am a new guy to java. I want to find the longest sequential same character array in a input character arrays. For example,this character array bddfDDDffkl, the longest is DDD, and this one: rttttDDddjkl, the longest is tttt.
I use the following code to deal with this problem. But, I want to improve my code, For example, if there are two same length arrays (for example rtttgHHH, there are two longest: ttt and HHH), how to solve this problem?
Thanks in advance.
My following code:
public class SeqSameChar {
public static void main (String[] args) {
int subLength = 0;
Scanner sc = new Scanner(System.in);
String[] num = null;
num = sc.nextLine().split(" ");
String[] number = new String[num.length];
for(int i = 0; i< number.length;i++) {
number[i] = String.valueOf(num[i]);
}
subLength =length(number,num.length);
System.out.println(subLength);
for(int i = index; i < index+subLength; i++) {
System.out.print(number[i]);
}
System.out.println(c==c1);
}
public static int index;
//to calculate the longest contiguous increasing sequence
public static int length(String[] A,int size){
if(size<=0)return 0;
int res=1;
int current=1;
for(int i=1;i<size;i++){
if(A[i].equals(A[i-1])){
current++;
}
else{
if(current>res){
index=i-current;
res=current;
}
current=1;
}
}
return res;
}
}
This algorithm will work perfectly fine for what you want to develop:
Before that, let me make it clear that if you want to check repeatitions of 2 different characters same number of times, you have to run a for loop in reverse to identify the 2nd character. So if the 2nd character is not same as the first one identified, and also if it's number of repeatitions are the same, you print both the characters or else, just print the single character you find at the first for loop because both the characters are going to be same.
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter String 1: ");
String A1 = sc.nextLine();
MaxRepeat(A1);
}
public static void MaxRepeat(String A) {
int count = 1;
int max1 = 1;
char mostrepeated1 = ' ';
for(int i = 0; i < A.length()-1;i++) {
char number = A.charAt(i);
if(number == A.charAt(i+1)) {
count++;
if(count>max1) {
max1 = count;
mostrepeated1 = number;
}
continue;
}
count = 1;
}
count = 1;
int max2 = 1;
char mostrepeated2 = ' ';
for(int i = A.length()-1; i>0; i--) {
char number = A.charAt(i);
if(number == A.charAt(i-1)) {
count++;
if(count>max2) {
max2 = count;
mostrepeated2 = number;
}
continue;
}
count = 1;
}
if((max1==max2) && (mostrepeated1==mostrepeated2)) {
System.out.println("Most Consecutively repeated character is: " + mostrepeated1 + " and is repeated " + max1 + " times.");
}
else if((max1==max2) && (mostrepeated1!=mostrepeated2)) {
System.out.println("Most continously repeated characters are: " + mostrepeated1 + " and " + mostrepeated2 + " and they are repeated " + max1 + " times");
}
}
I'll give you a Scala implementation for that problem.
Here it is the automatic test (in BDD style with ScalaTest)
import org.scalatest._
class RichStringSpec extends FlatSpec with MustMatchers {
"A rich string" should "find the longest run of consecutive characters" in {
import Example._
"abceedd".longestRun mustBe Set("ee", "dd")
"aeebceeedd".longestRun mustBe Set("eee")
"aaaaaaa".longestRun mustBe Set("aaaaaaa")
"abcdefgh".longestRun mustBe empty
}
}
Following is the imperative style implementation, with nested loops and mutable variables as you would normally choose to do in Java or C++:
object Example {
implicit class RichString(string: String) {
def longestRun: Set[String] = {
val chunks = mutable.Set.empty[String]
val ilen = string.length
var gmax = 0
for ((ch, curr) <- string.zipWithIndex) {
val chunk = mutable.ListBuffer(ch)
var next = curr + 1
while (next < ilen && string(next) == ch) {
chunk += string(next)
next = next + 1
}
gmax = chunk.length max gmax
if (gmax > 1) chunks += chunk.mkString
}
chunks.toSet.filter( _.length == gmax )
}
}
}
Following is a functional-style implementation, hence no variables, no loops but tail recursion with result accumulators and pattern matching to compare each character with the next one (Crazy! Isn't it?):
object Example {
implicit class RichString(string: String) {
def longestRun: Set[String] = {
def recurse(chars: String, chunk: mutable.ListBuffer[Char], chunks: mutable.Set[String]): Set[String] = {
chars.toList match {
case List(x, y, _*) if (x == y) =>
recurse(
chars.tail,
if (chunk.isEmpty) chunk ++= List(x, y) else chunk += y,
chunks
)
case Nil =>
// terminate recursion
chunks.toSet
case _ => // x != y
recurse(
chars.tail,
chunk = mutable.ListBuffer(),
chunks += chunk.mkString
)
}
}
val chunks = recurse(string, mutable.ListBuffer(), mutable.Set.empty[String])
val max = chunks.map(_.length).max
if (max > 0) chunks.filter( _.length == max ) else Set()
}
}
}
For example, for the given "aeebceeedd" string, both implementations above will build the following set of chunks (repeating characters)
Set("ee", "eee", "dd")
and they will filter those chunks having the maximum length (resulting "eee").

java.lang.NumberFormatException for input string

I'm creating a program which makes the given input string into number so that input will be coded. But I'm running into a NumberFormatException as soon as the input string gets too long. I can't see how I can fix this.
Note that I have to get substrings from the given string input, turn them into numericValues then get the sum of these two strings as an answer.
Code:
public class Puzzle {
private static char[] letters = {'a','b','c','d','e','f','g','h','i', 'j','k','l','m','n','o','p','q','r','s',
't','u','v','w','x','y','z'};
private static String input;
private static String delimiters = "\\s+|\\+|//+|=";
public static void main(String[]args)
{
input = "youuu + are = gay"; //as soon as the substrings before = sign are
//longer than 5 characters the exception occurs
System.out.println(putValues(input));
}
//method to put numeric values for substring from input
#SuppressWarnings("static-access")
public static long putValues(String input)
{
Integer count = 0;
long answer = 0;
String first="";
String second = "";
StringBuffer sb = new StringBuffer(input);
int wordCounter = Countwords();
String[] words = countLetters();
System.out.println(input);
if(input.isEmpty())
{
System.out.println("Sisestage mingi s6na");
}
if(wordCounter == -1 ||countLetters().length < 1){
return -1;
}
for(Character s : input.toCharArray())
{
for(Character c : letters)
{
if(s.equals(c))
{
count = c.getNumericValue(c);
System.out.print(s.toUpperCase(s) +"="+ count + ", ");
}
}
if(words[0].contains(s.toString()))
{
count = count - 1;
count = s.getNumericValue(s);
//System.out.println(count);
first += count.toString();
}
if(words[3].contains(s.toString())){
count = s.getNumericValue(s);
second += count.toString();
}
}
try {
answer = Integer.parseInt(first) + Integer.parseInt(second);
} catch(NumberFormatException ex)
{
System.out.println(ex);
}
System.out.println("\n" + first + " + " + second + " = " + answer);
return answer;
}
public static int Countwords()
{
String[] countWords = input.split(" ");
int counter = countWords.length - 2;
if(counter == 0) {
System.out.println("Sisend puudu!");
return -1;
}
if(counter > 1 && counter < 3) {
System.out.println("3 sõna peab olema");
return -1;
}
if(counter > 3) {
System.out.println("3 sõna max!");
return -1;
}
return counter;
}
//method which splits input String and returns it as an Array so i can put numeric values after in the
//putValue method
public static String[] countLetters()
{
int counter = 0;
String[] words = input.split(delimiters);
for(int i = 0; i < words.length;i++) {
counter = words[i].length();
if(words[i].length() > 18) {
System.out.println("Yhe s6na maksimaalne pikkus on 18 t2hem2rki ");
}
}
return words;
}
Integers in Java (as in many languages) are limited by a minimum and maximum value.
More information on this can be found here: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html
You could give a meaningful error in the catch-block
You did not enter a valid 32-bit Integer value.
Or you could switch to something like a BigDecimal which can hold bigger values: https://docs.oracle.com/javase/7/docs/api/java/math/BigDecimal.html
(watch out: BigDecimal works very different from a normal int, so read the documentation wisely, and Google for examples if necessary)
EDIT: you can parse it to Long as well, if you want that: Long.parseLong(INPUT, 10);. That way you extend the limit to 64-bit.

How to show all combinations of IP address that can be created from a string of numbers?

I need to format the input string into IP address format, so I have the following code;however,the numbers are fixed and I am not sure how to generated different values for a single input.
Other constraints would be to make sure no group of numbers is more than 255, but in this case I just want to put them in four separate groups and each group must have 1 to 3 members.
Vimal's question: From provided string 19216801, I think you cant identify exact ip. It can be 192.168.0.1 or 19.216.80.1 or any other combination.
Answer: I am not looking for any specific IP I just need to show all the possible combinations.
Sample formats
Some of the combinations would be as following
Expected result | number of input characters
1.1.1.1 4
....
1.1.1.2 5
1.1.2.1
1.2.1.1
2.1.1.1
....
1.1.1.3 6
1.1.3.1
1.3.1.1
3.1.1.1
....
2.2.2.1 7
2.2.1.2
....
2.2.2.2 8
3.2.2.1
1.2.2.3
....
2.2.2.3 9
3.3.2.1
1.2.3.3
....
3.3.3.1 10
3.3.1.3
3.1.3.3
1.3.3.3
....
3.3.3.2 11
3.3.2.3
3.2.3.3
....
3.3.3.3 12
Code
String number = "19216801";
if (number.length() == 4) {
StringBuilder sb = new StringBuilder(number)
.insert(1, ".")
.insert(1, ".")
.insert(1, ".")
.insert(1, ".");
String output = sb.toString();
System.out.println(output);
}
if (number.length() == 8) {
StringBuilder sb = new StringBuilder(number)
.insert(2, ".")
.insert(2, ".")
.insert(2, ".")
.insert(2, ".");
String output = sb.toString();
System.out.println(output);
}
if (number.length() == 12) {
StringBuilder sb = new StringBuilder(number)
.insert(3, ".")
.insert(3, ".")
.insert(3, ".")
.insert(3, ".");
String output = sb.toString();
System.out.println(output);
}
Rephrase task in next way.
imagine that ip part can have zero digit so ... is valid
then we have number.length() - 3 elements and need to put 3 dot in any position
let a, b, c be length of part
first part can be any length for(int a = 0; a < l; a++)
second one must be shorter for(int b = 0; b < l-a; b++)
same with third, total length must be l. so l>=a+b+c is constraint. c
put points in it places.
first poin just after first part (don't forget tat at first step we cut one digit from each part).
second is after first part, first dot and second part ((a +1) + 1 + (b+1))
third one the same. skip first part (a+1), dot (+1), second part (+b+1), second dot (+1) and third part (c+1) = a+b+c+5
String number = "19216801";
int l = number.length() - 3;
for(int a = 0; a < l; a++) {
for(int b = 0; b < l-a; b++){
for(int c = 0; c <l-a-b; c++){
StringBuilder sb = new StringBuilder(number);
sb.insert(a+1, ".");
sb.insert(a+b+3, ".");
sb.insert(a+b+c+5, ".");
System.out.println(sb);
}
}
}
It pretty difficult to explain, most of code come from background of my mind i just write it.
Without further information you have to rely on conjecture to form an IP address from a variable length string.
You should disallow that and ensure that your string is 12 characters long.
Once you've formed a candidate IP address though, you can validate it using the following regular expression (using String.matches)
\b(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.
(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.
(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.
(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b
public class IPAddress {
static Queue<List<StringBuilder>> queue=new LinkedList<List<StringBuilder>>();
static int count =0;
public static void main(String[] args) {
// TODO Auto-generated method stub
try(Scanner reader=new Scanner(System.in)){
String str=reader.nextLine();
if(init(str)==-1)
System.out.println("IPAddress cannot be formed");
ipAddress();
}
}
private static void ipAddress() {
// TODO Auto-generated method stub
int noOfGroups=4;
int group=noOfGroups-1;
int countInOneLevel=1, childCount=0;
while(!queue.isEmpty() && countInOneLevel>0 && group>0){
List<StringBuilder> list=queue.poll();
countInOneLevel--;
StringBuilder currentGroup=list.get(group);
StringBuilder prevGroup=list.get(group-1);
while(currentGroup.length()>1){
prevGroup.append(currentGroup.charAt(0));
currentGroup=currentGroup.deleteCharAt(0);
if(makeIPAdress(list, group)==1 ){
childCount++;
}
}
if(countInOneLevel==0){//current level complete
countInOneLevel=childCount;
group--;
childCount=0;
}
}
System.out.println("No. of possible IPAddress: "+count);
}
private static int init(String str) {
// TODO Auto-generated method stub
int length=str.length();
if(length<4 || length>12)
return -1;
StringBuilder strgroup[]= new StringBuilder[4];
int groups=4;
for(int i=0;i<groups-1;i++){
strgroup[i]=new StringBuilder(str.substring(i,i+1));
}
strgroup[groups-1]=new StringBuilder(str.substring(3,length));
List<StringBuilder> list=new ArrayList<StringBuilder>();
for(int i=0;i<groups;i++){
list.add(strgroup[i]);
}
return makeIPAdress(list,groups-1);
}
private static int makeIPAdress(List<StringBuilder> list, int i) {
// TODO Auto-generated method stub
if(isValidIPAdress(list)){
List<StringBuilder> list1=new ArrayList<StringBuilder>();
for(int k=0;k<4;k++){
StringBuilder s=new StringBuilder(list.get(k).toString());
list1.add(s);
}
queue.offer(list1);
display(list);
count++;
return 1;
}
for(int group=i;group>0;group--){
StringBuilder currentGroup=list.get(group);
StringBuilder prevGroup=list.get(group-1);
int num=Integer.parseInt(currentGroup.toString());
while(num<0|| num>255){
prevGroup.append(currentGroup.charAt(0));
currentGroup=currentGroup.deleteCharAt(0);
num=Integer.parseInt(currentGroup.toString());
}
}
StringBuilder firstGroup=list.get(0);
int num=Integer.parseInt(firstGroup.toString());
if(num>=0 && num<=255){
List<StringBuilder> list1=new ArrayList<StringBuilder>();
for(int k=0;k<4;k++){
StringBuilder s=new StringBuilder(list.get(k).toString());
list1.add(s);
}
queue.offer(list1);
display(list);
count++;
return 1;
}
return -1;
}
private static boolean isValidIPAdress(List<StringBuilder> list) {
// TODO Auto-generated method stub
for(int group=0;group<4;group++){
int num=Integer.parseInt(list.get(group).toString());
if(num<0 || num>255)
return false;
}
return true;
}
private static void display(List<StringBuilder> list) {
// TODO Auto-generated method stub
Iterator<StringBuilder> i=list.iterator();
while(i.hasNext()){
StringBuilder s=i.next();
if(!i.hasNext())
System.out.print(s);
else
System.out.print(s+".");
}
System.out.println();
}
}
Sample Input:
2252555
Sample Output:
2.25.25.55
2.25.255.5
2.252.55.5
225.25.5.5
22.52.55.5
225.2.55.5
No. of possible IPAddress: 6
Here is a recursive solution:
public static void main(String[] args){
System.out.println(findIPs("1234567", 3));
}
public static List<String> findIPs(String s,int dots){
List<String> ips = new ArrayList<>();
for(int i =1 ;i<=3 && i < s.length(); i++){
String cip = s.substring(0,i);
if(Integer.parseInt(cip) < 256){
if(dots == 1){
if( Integer.parseInt(s.substring(i)) < 256) {
ips.add(cip + "." + s.substring(i));
}
}else {
for (String ip : findIPs(s.substring(i), dots - 1)) {
ips.add(cip + "." + ip);
}
}
}
}
return ips;
}
Here is the solution to resolve the wrong ips issue of one of the above solutions
private static List<String> ips = new ArrayList<>();
public static void main(String[] args) {
Date d = new Date();
System.out.println(posIps("19216801"));
System.out.println(new Date().getTime() - d.getTime());
}
private static List<String> posIps(String number) {
int l = number.length() - 3;
for (int a = 0; a < 3 && a < l; a++) {
for (int b = 0; b < 3 && b < l - a; b++) {
for (int c = 0; c < 3 && c < l - a - b; c++) {
StringBuilder sb = new StringBuilder(number);
if (Integer.parseInt(sb.substring(0, a + 1 )) < 256
&& Integer.parseInt(sb.substring(a + 1, a + b + 2)) < 256
&& Integer.parseInt(sb.substring(a + b + 2, a + b + c + 3)) < 256
&& Integer.parseInt(sb.substring(a + b + c + 3)) < 256) {
sb.insert(a + 1, ".");
sb.insert(a + b + 3, ".");
sb.insert(a + b + c + 5, ".");
ips.add(sb.toString());
}
}
}
}
return ips;
}
This code works fine, check it.
public static void main(String[] args) {
String input = "121212111";
for (String ipAddress : generatePossibleIpAddresses(input, 3)) {
System.out.println(ipAddress);
}
}
public static ArrayList<String> generatePossibleIpAddresses(String ipAddress, int dot) {
ArrayList<String> list = new ArrayList<String>();
if (ipAddress == null || ipAddress.length() == 0) {
return list;
}
if (dot == 0) {
int i = Integer.parseInt(ipAddress);
if (i < 256) {
list.add(ipAddress);
}
return list;
}
for (int i = 1; i <= 3; i++) {
int num = Integer.parseInt(ipAddress.substring(0, i));
if (num < 256) {
for (String str : generatePossibleIpAddresses(ipAddress.substring(i), dot - 1)) {
list.add(num + "." + str);
}
}
}
return list;
}
private static String getValidIp(List<Integer> combination, String ip) {
int from = 0;
int to = 0;
String finalIp = "";
for (int digit : combination) {
to += digit;
String ipPart = ip.substring(from, to);
if (!isValidIp(ipPart)) {
return null;
}
finalIp += ipPart + ".";
from = to;
}
return finalIp.replaceAll(".$", "");
}
public static List<List<Integer>> getOptions(String ip) {
List<Integer> baseOption = Arrays.asList(1, 2, 3);
List<List<Integer>> options = new ArrayList<>();
baseOption.forEach(i -> {
baseOption.forEach(i2 -> {
baseOption.forEach(i3 -> {
baseOption.forEach(i4 -> {
if (isRelevantOption(ip, i + i2 + i3 + i4)) {
options.add(Arrays.asList(i, i2, i3, i4));
}
});
});
});
});
return options;
}
private static boolean isRelevantOption(String ip, int sum) {
return ip.length() == sum;
}
private static boolean isValidIp(String ip) {
return Integer.parseInt(ip) < 256;
}
public static List<String> GetAllValidIpAddress(String ip) {
if (ip.length() > 12) {
System.out.println("IP is not valid");
}
List<List<Integer>> options = getOptions(ip);
return options.stream().map(c -> getValidIp(c, ip)).filter(Objects::nonNull).collect(Collectors.toList());
}
public static void main(String args[]) {
GetAllValidIpAddress("2562547").forEach(ip -> System.out.println(ip));
}

Categories