Exception : java.lang.NumberFormatException
Bonjour,
j'ai un programme qui parcourt un fichier csv dans le but de récupérer des données.
La classe csvFile se charge de récupérer les cellules qui sont des String.
Lorsque j'affiche les cellules sous la forme de String pas de soucis, aucune exception se produit :
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 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
| "30/04/2007"
04
nb jours = 30
"01/04/2007 *"
"02/04/2007 "
"03/04/2007 "
"04/04/2007 "
"05/04/2007 "
"06/04/2007 "
"07/04/2007 *"
"08/04/2007 *"
"09/04/2007 *"
"10/04/2007 "
"11/04/2007 "
"12/04/2007 "
"13/04/2007 "
"14/04/2007 *"
"15/04/2007 *"
"16/04/2007 "
"17/04/2007 "
"18/04/2007 "
"19/04/2007 "
"20/04/2007 "
"21/04/2007 *"
"22/04/2007 *"
"23/04/2007 "
"24/04/2007 "
"25/04/2007 "
"26/04/2007 "
"27/04/2007 "
"28/04/2007 *"
"29/04/2007 *"
"30/04/2007 "
---------------------
"0"
"2"
"1"
"1"
"3"
"5"
"0"
"0"
"0"
"4"
"2"
"5"
"7"
"0"
"0"
"7"
"2"
"3"
"8"
"3"
"0"
"0"
"1"
"0"
"1"
"1"
"0"
"0"
"0"
"0"
---------------------
"0"
"0"
"0"
"0"
"0"
"0"
"0"
"0"
"0"
"0"
"0"
"0"
"0"
"0"
"0"
"0"
"0"
"0"
"0"
"0"
"0"
"0"
"0"
"0"
"0"
"0"
"0"
"0"
"0"
"0" |
Mais lorsque je convertis ces String en Integer, là, j'obtiens :
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
| "30/04/2007"
04
nb jours = 30
"01/04/2007 *"
"02/04/2007 "
"03/04/2007 "
"04/04/2007 "
"05/04/2007 "
"06/04/2007 "
"07/04/2007 *"
"08/04/2007 *"
"09/04/2007 *"
"10/04/2007 "
"11/04/2007 "
"12/04/2007 "
"13/04/2007 "
"14/04/2007 *"
"15/04/2007 *"
"16/04/2007 "
"17/04/2007 "
"18/04/2007 "
"19/04/2007 "
"20/04/2007 "
"21/04/2007 *"
"22/04/2007 *"
"23/04/2007 "
"24/04/2007 "
"25/04/2007 "
"26/04/2007 "
"27/04/2007 "
"28/04/2007 *"
"29/04/2007 *"
"30/04/2007 "
---------------------
"0"
Exception in thread "main" java.lang.NumberFormatException: For input string: ""0""
at java.lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at Test.affiche(Test.java:62)
at Test.main(Test.java:83) |
La ligne 62 est :
Code:
Integer i = Integer.parseInt(s);
Dans le bout de code suivant
Code:
1 2 3 4 5 6 7 8
| List<Integer> liste_repondus = new ArrayList<Integer>();
for(int nlignes=min; nlignes<max; nlignes++){
String s = monFichier.getData(nlignes,5);
System.out.println(s);
Integer i = Integer.parseInt(s);
System.out.println(i+" ");
liste_repondus.add(i);
} |
Je n'arrive pas trop à comprendre pourquoi survient cette exception
Classe CSVFile (ça vient des sources de developpez.com)
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 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
| import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
import java.util.Vector;
public class CSVFile {
private int m_rowsCount;
private int m_colsCount;
private Vector<Object> m_fileContent;
private final static char CELL_SEPARATOR = ';';
/**
* Method CSVFile.
* @param path le chemin du fichier à parser.
* @throws FileNotFoundException si le fichier spécifié n'existe pas.
*/
public CSVFile(String path) throws FileNotFoundException {
m_fileContent = new Vector<Object>();
FileReader fileReader = new FileReader(path);
readFromFile(fileReader);
fitVectorsToSize();
}
/**
* Method CSVFile.
* @param reader un reader dans lequel on lit le fichier CSV.
*/
public CSVFile(Reader reader) {
m_fileContent = new Vector<Object>();
readFromFile(reader);
fitVectorsToSize();
}
private void fitVectorsToSize() {
m_fileContent.setSize(getRowsCount());
int fileSize = getRowsCount();
int colCount = getColsCount();
for (int i = 0; i < fileSize; i++) {
Vector aRow = (Vector)m_fileContent.get(i);
if (aRow == null) {
m_fileContent.set(i, new Vector());
aRow = (Vector)m_fileContent.get(i);
}
aRow.setSize(colCount);
}
}
/**
* Method readFromFile.
* @param path
*/
private void readFromFile(Reader reader) {
BufferedReader buffReader = new BufferedReader(reader);
if (buffReader != null) {
try {
String tempLine;
tempLine = buffReader.readLine();
while (tempLine != null) {
readFromLine(tempLine);
tempLine = buffReader.readLine();
}
} catch (IOException e) {
System.err.println("Error reading CSV file: " + e.toString());
} finally {
try {
buffReader.close();
} catch (IOException e) {
System.err.println(
"Erreur closing CSV file: "
+ e.toString()
);
}
}
}
System.runFinalization();
System.gc();
}
/**
* Method readFromLine.
* @param tempLine
*/
private void readFromLine(String tempLine) {
if (tempLine == null) {
return;
}
Vector<Object> currentLine = new Vector<Object>();
m_fileContent.add(currentLine);
m_rowsCount++;
// setRowsCount(getRowsCount() + 1);
if (tempLine.trim().length() == 0) {
return;
}
int colCount = 0;
int cursorBegin = 0;
int cursorEnd = tempLine.indexOf(CELL_SEPARATOR);
while (cursorBegin > -1) {
if (cursorEnd == -1) {
currentLine.add(tempLine.substring(cursorBegin));
cursorBegin = cursorEnd;
} else {
currentLine.add(tempLine.substring(cursorBegin, cursorEnd));
cursorBegin = cursorEnd + 1;
}
cursorEnd = tempLine.indexOf(CELL_SEPARATOR, cursorBegin);
colCount++;
}
if (colCount > getColsCount()) {
setColsCount(Math.max(getColsCount(), colCount));
}
}
/**
* Returns the colsCount.
* @return int
*/
public int getColsCount() {
return m_colsCount;
}
/**
* Returns the rowsCount.
* @return int
*/
public int getRowsCount() {
return m_rowsCount;
}
/**
* Sets the colsCount.
* @param colsCount The colsCount to set
*/
public void setColsCount(int colsCount) {
m_colsCount = colsCount;
fitVectorsToSize();
}
/**
* Sets the rowsCount.
* @param rowsCount The rowsCount to set
*/
public void setRowsCount(int rowsCount) {
m_rowsCount = rowsCount;
fitVectorsToSize();
}
/**
* Method getData.
* @param row la ligne voulue
* @param col la colonne voulue
* @return String la valeur à l'enplacement spécifié. Null si outOfBound.
*/
public String getData(int row, int col) {
if (row < 0
|| col < 0
|| row > (getRowsCount() - 1)
|| col > (getColsCount() - 1)) {
return null;
}
try {
Vector theRow = (Vector)m_fileContent.get(row);
String result = (String)theRow.get(col);
return (result == null ? "" : result);
} catch (IndexOutOfBoundsException e) {
return "";
}
}
/*public static void main(String [] args){
try{
CSVFile monFichier = new CSVFile("D:\\Documents and Settings\\dsingh\\Desktop\\Stats jour PHL CSD_070307_171503_sgacthr.csv");
String valeur = monFichier.getData(0, 0);
System.out.println(valeur);
monFichier = null;
}catch(FileNotFoundException fnfe){
fnfe.printStackTrace();
}
}*/
} |
classe Test :
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 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
| import java.io.IOException;
import java.util.ArrayList;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class Test{
public static void affiche(String fileName){
try{
CSVFile monFichier = new CSVFile(fileName);
String date = monFichier.getData(3,1);
System.out.println(date);
//String mois = date.substring(3,5);
String mois = date.substring(4,6);
System.out.println(mois);
Map<String, Integer> map = new HashMap<String, Integer>();
map.put("01",31);
map.put("03", 31);
map.put("04", 30);
map.put("05", 31);
map.put("06", 30);
map.put("07", 31);
map.put("08", 31);
map.put("09", 30);
map.put("10", 31);
map.put("11", 30);
map.put("12", 31);
int val;
if(mois.equals("02")){
String annee = date.substring(6,10);
Integer year = Integer.parseInt(annee);
GregorianCalendar gc = new GregorianCalendar();
if(gc.isLeapYear(year))
val = 28;
else
val = 29;
}
else{
val = map.get(mois);
System.out.println("nb jours = " + val);
}
int min = 11;
int max=val+min;
List<String> liste_jours = new ArrayList<String>();
for(int nlignes=min; nlignes<max; nlignes++){
String string = monFichier.getData(nlignes,0);
System.out.println(string);
liste_jours.add(string);
}
System.out.println("---------------------");
List<Integer> liste_repondus = new ArrayList<Integer>();
for(int nlignes=min; nlignes<max; nlignes++){
String s = monFichier.getData(nlignes,5);
System.out.println(s);
Integer i = Integer.parseInt(s);
System.out.println(i+" ");
liste_repondus.add(i);
}
System.out.println("---------------------");
List<Integer> liste_debordement = new ArrayList<Integer>();
for(int nlignes=min; nlignes<max; nlignes++){
String string = monFichier.getData(nlignes,6);
System.out.println(string);
Integer i = Integer.parseInt(string);
System.out.println(i+" ");
liste_debordement.add(i);
}
}catch(IOException ioe){
ioe.printStackTrace();
}
}
public static void main(String [] args){
Test.affiche("fichier_test.csv");
}
} |