Android : How to find a dynamic Match with a Pattern

on Friday, March 27, 2015


Ok, so i have been looking for the answer a little bit but cant find any sort of answer to this:


I am creating a file on my mobile and want to write the following:


If a line with this pattern "N:xval,yval" exists for the number N, and furthermore xval and yval arent set, then i want to replace that line with "N:xval+1,yval+{0 or 1}" If there is no line like this i want to create a new line with "N:1,{0 or 1}"


THE ISSUE: it doesnt find a Match


i am at this state:



for (int N=0; N<myNumber; N++){
Pattern p = Pattern.compile("(\\d+):(\\d+),(\\d+)");
Matcher m = p.matcher(""+myNumber+":(\\d+),(\\d+)");
m.find();

int n, k, val;
if (m.matches()) {
val =Integer.parseInt(m.group(1));
n = Integer.parseInt(m.group(2));
k = Integer.parseInt(m.group(3));
++n;
if (myBoolean==true){
++k;
}
line = String.format("%d:%d,%d", val ,n, k);
try {
globalwriter.write(line.getBytes(), m.start(), line.length());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}else{
int l;
if (myBool==true){
l =1;
}else{
l=0;
}
line = String.format("%d:%d,%d \n ", myNumber ,1, l);
try {
int end;
if (checker.length() == 0){
end = 0;
}else{
end = (int)checker.length() -1;
}
globalwriter.write(line.getBytes(),(int) end,line.length());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}


Thanks for replys, and please dont donate and hate :D


0 comments:

Post a Comment