Mon code ne fonctionne pas. Le fichier texte est dans le même dossier que mes classes. J'ai utilisé le chemin d'accès, qui a fonctionné, mais je ne pense pas que cela fonctionnerait si j'envoyais le fichier à quelqu'un d'autre. Et convertir les chaînes en type primitif à l'aide de méthodes d'analyse ne fonctionne pas non plus. Je ne sais pas ce que je fais mal. Quelqu'un peut-il aider?Comment résoudre 'NumberFormatException.forInputString()'?
Voici mon code:
import java.util.Scanner;
import java.util.StringTokenizer;
import java.io.FileNotFoundException;
import java.io.FileInputStream;
public class TestInventory {
public static void main(String[] args) {
// TODO Auto-generated method stub
Inventory movieList = new Inventory();
Scanner inputStream = null;
try{
inputStream = new Scanner(new FileInputStream("movies_db.txt"));
}
catch(FileNotFoundException e){
System.out.println("File not found or could not be opened");
System.exit(0);
}
while(inputStream.hasNextLine()){
String s = inputStream.nextLine();
StringTokenizer st = new StringTokenizer(s, " - ");
String t1 = st.nextToken();
String t2 = st.nextToken();
String t3 = st.nextToken();
String t4 = st.nextToken();
int y = Integer.parseInt(t2);
double r = Double.parseDouble(t4);
int d = Integer.parseInt(t3);
Movie m = new Movie(t1, y, r, d);
movieList.addMovie(m);
}
}
}
Et ceci est la sortie que je reçois:
run:
Exception in thread "main" java.lang.NumberFormatException: For input string: "America:"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:580)
at java.lang.Integer.parseInt(Integer.java:615)
at TestInventory.main(TestInventory.java:29)
C:\Users\customer\AppData\Local\NetBeans\Cache\8.1\executor-snippets\run.xml:53: Java returned: 1
BUILD FAILED (total time: 0 seconds)
Quelle est la sortie que vous obtenez? – Subu
run: Exception dans le thread "principal" java.lang.NumberFormatException: Pour chaîne d'entrée: "L'Amérique:" \t à java.lang.NumberFormatException.forInputString (NumberFormatException.java:65) \t à java.lang.Integer. parseInt (Integer.java:580) \t à java.lang.Integer.parseInt (Integer.java:615) \t à TestInventory.main (TestInventory.java:29) C: \ Users \ client \ AppData \ local \ NetBeans \ Cache \ 8.1 \ executor-snippets \ run.xml: 53: Java renvoyé: 1 BUILD FAILED (temps total: 0 secondes) –
@JJoseph à quoi ressemble 'movies_db.txt'? – Arthur