I have a formatted text like this:
x.i9j11k2d1" index="603" value="0"/>
x.i9j11k2d2" index="604" value="0"/>
x.i9j11k2d3" index="605" value="0"/>
x.i10j1k1d1" index="606" value="-0"/>
And, I'm interested in Scanning only the digits. For example:
int i,j,k,d,index,value;
For the first line I want:
i=9, j=11, k=2, d=1, index=603, value=0
For this purpose, I used the following code:
Scanner file=new Scanner(new File("C:/sol.txt"));
while(file.hasNext())
{
System.out.println("");
int i=0;
int j=0;
int k=0;
int d=0;
file.useDelimiter("");
while(!(file.hasNextInt()))
{
System.out.print(file.next());
}
//System.out.print(file.next());
file.useDelimiter("j");
i=file.nextInt();
System.out.print(i);
file.useDelimiter("");
System.out.print(file.next()); //j
file.useDelimiter("k");
j=file.nextInt();
System.out.print(j);
file.useDelimiter("");
System.out.print(file.next()); //k
k=file.nextInt();
System.out.print(k);
System.out.print(file.next()); //d
d=file.nextInt();
System.out.print(d);
while(!(file.hasNextInt()))
{
System.out.print(file.next());
}
file.useDelimiter("\"");
int index=file.nextInt();
System.out.print(index);
file.useDelimiter("");
while(!(file.hasNextInt()))
{
System.out.print(file.next());
}
int value=file.nextInt();
System.out.print(value);
System.out.print(file.nextLine());
}
file.close();
}
catch (FileNotFoundException exc)
{System.out.println("File non trovato");}
It works perfectely until i
is one digit, but then, when i have to scan the fourth line, I don't know why it returns the following:
...
//System.out.print(file.next());
file.useDelimiter("j");
i=file.nextInt(); // it returns i=1 instead of 10
System.out.print(i);
file.useDelimiter("");
System.out.print(file.next()); //j --> prints 0 instead of j
file.useDelimiter("k");
j=file.nextInt(); // --> finds j instead of 1 and
// crashes with "InputTypeMismatch"
the file is formatted in XML, i cannot post the entire file cause it's thousands of lines, but it's like the following:
<?xml version = "1.0" encoding="UTF-8" standalone="yes"?>
<CPLEXSolution version="1.2">
<header
problemName="prob"
solutionName="incumbent"
solutionIndex="-1"
objectiveValue="58.2123812523709"
solutionTypeValue="3"
solutionTypeString="primal"
solutionStatusValue="102"
solutionStatusString="integer optimal, tolerance"
solutionMethodString="mip"
primalFeasible="1"
dualFeasible="0"
MIPNodes="3285"
MIPIterations="22164"
writeLevel="1"/>
<variables>
<variable name="x.i0j1k1d1" index="0" value="0"/>
<variable name="x.i0j1k1d2" index="1" value="0"/>
<variable name="x.i0j1k1d3" index="2" value="0"/>
<variable name="x.i0j1k2d1" index="3" value="1"/>
</variables>
</CPLEXSolution>
To make it simple use regex. With pattern "\d+" it will extract all numbers which you can use as you need it. Look at the code. Pattern p matches next digit, Matcher m applies this pattern to the string and then m.find() method extracts next group (digit number with pattern \d) and here is your number.
import java.util.regex.*;
public class test {
public static void main(String[] args) throws Exception {
int i = 0,j,k,d,index,value = 0;
Pattern p = Pattern.compile("-?\\d+");
Matcher m = p.matcher("x.i9j11k2d1\" index=\"603\" value=\"010\"/>");
if(m.find()) i=Integer.parseInt(m.group());
if(m.find()) j=Integer.parseInt(m.group());
if(m.find()) k=Integer.parseInt(m.group());
if(m.find()) d=Integer.parseInt(m.group());
if(m.find()) index=Integer.parseInt(m.group());
if(m.find()) value=Integer.parseInt(m.group());
System.out.println("i="+i+" value="+value);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With