java.util.scanner - Scanning numbers from a formatted text in java -


i have formatted text 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 digits. example:

int i,j,k,d,index,value; 

for first line want:

i=9, j=11, k=2, d=1, index=603, value=0 

for purpose, used 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 1 digit, then, when have scan fourth line, don't know why returns following:

... //system.out.print(file.next()); file.usedelimiter("j"); i=file.nextint();                   // 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 ,                                       //         crashes "inputtypemismatch" 

the file formatted in xml, cannot post entire file cause it's thousands of lines, it's 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 simple use regex. pattern "\d+" extract numbers can use need it. @ code. pattern p matches next digit, matcher m applies pattern string , m.find() method extracts next group (digit number pattern \d) , here number.

import java.util.regex.*;  public class test {     public static void main(string[] args) throws exception {         int = 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);     } } 

Comments

Popular posts from this blog

IF statement in MySQL trigger -

c++ - What does MSC in "// appease MSC" comments mean? -

javascript - Blogger related post gadget image Resize s72-c [ Need Expert Help ] -