2015-05-11 15 views
8

ho un testo formattato come questo:numeri Scansione da un testo formattato in java

x.i9j11k2d1" index="603" value="0"/> 
x.i9j11k2d2" index="604" value="0"/> 
x.i9j11k2d3" index="605" value="0"/> 
x.i10j1k1d1" index="606" value="-0"/> 

E, io sono interessato a scansione solo le cifre. Per esempio:

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

Per la prima linea che voglio:

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

A tal fine, ho usato il seguente codice:

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");} 

Funziona perfectely fino i è una cifra, ma poi, quando devo scansionare la quarta riga, non so perché restituisca quanto segue:

... 
//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" 

il file viene formattato in XML, non riesco a postare l'intero file perché è migliaia di linee, ma è come il seguente:

<?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> 
+0

Probabilmente stai meglio usando una regex per questo tipo di problema. – JonasCz

+0

Il problema sembra essere con il delimitatore 'file.useDelimiter (" ")' che non usa nulla (dimensione zero) come delimitatore. Meglio usare RE. –

+2

Questa è una delle migliori domande che ho visto da un nuovo utente per un po '. Godspeed. – snickers10m

risposta

2

Per rendere più semplice l'uso regex. Con il pattern "\ d +" estrarrà tutti i numeri che puoi usare quando ne hai bisogno. Guarda il codice. Modello p corrisponde alla cifra successiva, Corrispondente m applica questo modello alla stringa e quindi il metodo m.find() estrae il gruppo successivo (numero di cifre con motivo \ d) ed ecco il tuo numero.

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); 
    } 
} 
+0

grazie per la risposta, ma non sono sicuro di averlo capito (non ho familiarità con regex). Puoi aggiungere qualche spiegazione al tuo codice? – jcsun

+0

Fammi sapere se ne hai bisogno di più. Ma, fondamentalmente, puoi usarlo così com'è. – Alex

+0

Che funziona perfettamente! Grazie mille! dovrei assolutamente studiare la regex più profonda!comunque, solo per scopi educativi, mi piacerebbe capire perché il mio metodo non funziona. Quindi se qualcuno mi può illuminare sarei molto grato! – jcsun

Problemi correlati