i’m working on a method that split a string based on the . delimiter, i have used the Pattern and Matcher classes, and get the start positions of the delimiter and stored them in an array and now i want to split the specified string based on those start positions, my problem is that when i tried the following code, the program goes forever (Infinite loop).
public void cutByRegex(){
String outPut="";
int i=0;
int startIndex[]=new int[3];
System.out.println("IP--->"+ip);
Pattern p=Pattern.compile("\\.");
Matcher m=p.matcher(ip);
while (m.find()){
startIndex[i]=m.start();
i++;
System.out.println("start: "+m.start());
}
System.out.println("StartIndices-->");
for(int j:startIndex)
System.out.println(j);
for(i=0;i<startIndex.length+1;){
switch(i){
case 0:
outPut+=ip.substring(i,startIndex[i]);
i++;
case 1:
outPut+=ip.substring(startIndex[i]-1,startIndex[i]);
i++;
case 2:
outPut+=ip.substring(startIndex[i]-1,startIndex[i]);
outPut+=ip.substring(startIndex[i],ip.length());
break;
}
System.out.println("group--->"+outPut);
}
}
for example:
the startIndex array contains 3,5,7
i want to substring 127.1.1.254
from 0—>3
from 4—>5
from 6—>7
from 8—>ip.length
Note:i know the built in method split() well, i want to do the job manually
what mistakes i have done??
You have not used increment part in for loop i variable and case block 2
Your code