Problème de conversion d'un String en double
Bonjour je rencontre un petit problème qui m'empêche d'avancer comme je le souhaite:
je dévellope actuellement un petite interface qui va traiter des données excel calculer depuis imageJ.
Pour ce faire j'enregistre la table de résultats de imageJ au format .csv puis ensuite j'ai utilisé un programme d'un utilisateur qui traite les données du .csv
afin de remetre en ordre mes données :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
| ArrayList arList=null;
ArrayList al=null;
String fName = a; // avant j'ai programmé la demande d'ouverture de fichier stocker dans la variable String a
String thisLine;
int count=0;
FileInputStream fis = new FileInputStream(fName);
DataInputStream myInput = new DataInputStream(fis);
int icv=0;
arList = new ArrayList();
while ((thisLine = myInput.readLine()) != null)
{
al = new ArrayList();
String strar[] = thisLine.split(",");
for(int j=0;j<strar.length;j++)
{
al.add(strar[j]);
}
arList.add(al);
System.out.println();
icv++;
}
try
{
HSSFWorkbook hwb = new HSSFWorkbook();
HSSFSheet sheet = hwb.createSheet("new sheet");
for(int k=0;k<arList.size();k++)
{
ArrayList ardata = (ArrayList)arList.get(k);
HSSFRow row = sheet.createRow((short) 0+k);
for(int p=0;p<ardata.size();p++)
{
HSSFCell cell = row.createCell((short) p);
String data = ardata.get(p).toString();
if(data.startsWith("=")){
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
data=data.replaceAll("\"", "");
data=data.replaceAll("=", "");
data=data.replace(".", ",");
cell.setCellValue(data);
}else if(data.startsWith("\"")){
data=data.replaceAll("\"", "");
data=data.replace(".", ",");
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellValue(data);
}else{
data=data.replaceAll("\"", "");
data=data.replace(".", ",");
cell.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
cell.setCellValue(data);
}
//*/
// cell.setCellValue(ardata.get(p).toString());
}
System.out.println();
}
icsv1= a.indexOf(".csv");
a=a.substring(0,icsv1)+".xls";
FileOutputStream fileOut = new FileOutputStream(a);
hwb.write(fileOut);
fileOut.close();
System.out.println("Your "+a+ " excel file has been generated");
} catch ( Exception ex ) {
ex.printStackTrace();
} //main method ends |
Seulement par cette méthode tous le fichier est au format Text et j'aimerai que dans ma feuille excel les nombre devienne donc des nombre au format Double.
En cherchant sur le net et sur le site j'ai essayer de régler le problème avec une petite boucle;
Code:
1 2 3 4 5 6 7 8 9 10 11 12
|
double newData;
for (int col=3; col < lastColumn_cvs ; col++) {
for (int i=1; i<LastLigne_cvs+1;i++) {
cell_cvs = sheet_cvs.getRow(i).getCell((short)col);
String data=cell_cvs.getStringCellValue();
if (data.startsWith("")){
newData=Double.parseDouble(data);
cell_cvs.setCellValue(newData);
}// les cellules choisie comportent les données que je veut récupéré |
Seulement je reçoit l'erreur :
Citation:
Exception in thread "main" java.lang.NumberFormatException: For input string: "6107,854"
at sun.misc.FloatingDecimal.readJavaFormatString(Unknown Source)
at sun.misc.FloatingDecimal.parseDouble(Unknown Source)
at java.lang.Double.parseDouble(Unknown Source)
at Excel.main(Excel.java:355)
Je débute en java et pour le coup je n'arrive pas à m'en sortir cette fois-ci.
Je vous remercie d'avance pour l'aide que vous pourrez m'apporter.