Pb ligne.IndexOf()

kilian61 - 6 sept. 2012 à 15:54
cs_Julien39 Messages postés 6414 Date d'inscription mardi 8 mars 2005 Statut Modérateur Dernière intervention 29 juillet 2020 - 7 sept. 2012 à 09:23
Bonjours a tous ,

Donc voila mon pb je dois récupérer des données d'un txt.
J'utilise donc une condition if pour trouve le text avant celui que je dois trouver puis je fais un substring pour enregistrer le mot a recuperer.
Les position du substring utilise les index.of() du caractere precedent et suivant du mot a recupere

Seul problem mes ligne ne sont pas tjrs de la meme longeure et j ai donc defois des indexof du caractere suivant qui est egal a -1
Jusqua present j ai essaye de rajoute des blanc pour avoir le meme nombre de ligne partout mais je n arrive pas .
Donc je voudrai savoir comment faire pour que le indexof du suivant soit juiste ou comment identifier le caractere suivant pour recupere mon mot

Merci de votre aide voici le code

if(stri.equals(wunch)) {
cell.setCellValue("Modul");
row.createCell(1).setCellValue("Wunsche");
//Read Text Document
InputStream inpStre=new FileInputStream(str); 

InputStreamReader inpStreRead=new InputStreamReader(inpStre);

BufferedReader bufRead = new BufferedReader(inpStreRead);

while((ligne=bufRead.readLine())!=null){
boolean found = false;

if(ligne.indexOf(srtfound) !=- 1){
//Steck found 
mdl = ligne.substring(1, 9) ;
System.out.println(mdl);
}
else if (ligne.indexOf(when) !=-1 ){
//Modul found
int i = ligne.indexOf(when);
 int j;
  int l = ligne.length();
  System.out.println(l);

  j = ligne.indexOf(quote);
cpbook = ligne.substring(i+6, j);

found = true;
System.out.println(" debut "+i);
System.out.println(" longueur fin "+j);

}

if (found == true){
//Create the Excel Line
rowNb ++;
HSSFRow rw = sheet.createRow(rowNb);
HSSFCell cel0 = rw.createCell(0);
cel0.setCellValue(mdl);
HSSFCell cel1 = rw.createCell(1);
cel1.setCellValue(cpbook);

}

1 réponse

cs_Julien39 Messages postés 6414 Date d'inscription mardi 8 mars 2005 Statut Modérateur Dernière intervention 29 juillet 2020 371
7 sept. 2012 à 09:23
Salut,

Je pense que tu t'embêtes pour pas grand chose, qu'est ce que tu veux faire au juste. A mon avis l’utilisation d'une regex est plus appropriée. Si tu m'expliques ce que tu cherches à faire, je te donne la regex.
0
Rejoignez-nous