IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Entrée/Sortie Java Discussion :

Pb de lecture dans un fichier texte


Sujet :

Entrée/Sortie Java

  1. #1
    Nouveau membre du Club
    Profil pro
    Inscrit en
    Avril 2010
    Messages
    55
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Avril 2010
    Messages : 55
    Points : 37
    Points
    37
    Par défaut Pb de lecture dans un fichier texte
    Bonjour à tous,
    je cherche à exploiter les données dans un fichier texte de la forme suivante:
    2000
    (
    (0.999526 0.000683934 0)
    (0.988978 0.00114476 0)
    (0.989125 0.00116963 0)
    ... et un peu moins de 2000 autres valeurs

    J'utilise le code suivant:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    import java.io.*;
    import java.util.*; // pour StringTokeniser
     
    public class Main {
    	// Lecture dans un fichier texte
    	public static void main (String args[]) throws IOException
    	{
    		String nomfich;
    		double x,y,z;
    		nomfich="U.txt";
    		BufferedReader entree = new BufferedReader (new FileReader (nomfich));
    		System.out.println("Nombres contenus dans le fichier "+ nomfich +":");
    		while (true)
    		{String ligneLue = entree.readLine();
    		if (ligneLue==null) break;
    		StringTokenizer tok= new StringTokenizer (ligneLue," ");
    		x=Double.parseDouble(tok.nextToken());
    		y=Double.parseDouble(tok.nextToken());
    		z=Double.parseDouble(tok.nextToken());
    		System.out.println (x + " "+y+" "+z);
    		}
     
    		entree.close ();
    		System.out.println("fin liste fichier"+nomfich);
    	}
    	}
    Et j'obtiens le message d'erreur suivant:
    Exception in thread "main" java.lang.NumberFormatException: For input string: "("
    at sun.misc.FloatingDecimal.readJavaFormatString(Unknown Source)
    at java.lang.Double.parseDouble(Unknown Source)

    Comme je débute en java, je ne sais comment je pourrais ne pas supprimer les parenthèses ou ne pas en tenir compte dans mon "tokenizer"afin d'exploiter mes données numériques??

    merci d'avance

  2. #2
    Membre actif Avatar de pendoRa
    Homme Profil pro
    Ingénieur intégration
    Inscrit en
    Mai 2007
    Messages
    317
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 35
    Localisation : France, Rhône (Rhône Alpes)

    Informations professionnelles :
    Activité : Ingénieur intégration
    Secteur : Industrie

    Informations forums :
    Inscription : Mai 2007
    Messages : 317
    Points : 278
    Points
    278
    Par défaut
    Essaye ceci :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
     import java.io.*;
    import java.util.*; // pour StringTokeniser
     
    public class Lecture {
    	// Lecture dans un fichier texte
    	public Lecture() throws IOException
    	{
    		String nomfich;
    		double x,y,z;
    		nomfich="U.txt";
    		BufferedReader entree = new BufferedReader (new FileReader (nomfich));
    		System.out.println("Nombres contenus dans le fichier "+ nomfich +":");
    		while (true)
    		{String ligneLue = entree.readLine();
    		if (ligneLue==null) break;
    		StringTokenizer tok= new StringTokenizer (ligneLue," ");
    		x=Double.parseDouble(tok.nextToken());
    		y=Double.parseDouble(tok.nextToken());
    		z=Double.parseDouble(tok.nextToken());
    		System.out.println (x + " "+y+" "+z);
    		}
     
    		entree.close ();
    		System.out.println("fin liste fichier"+nomfich);
    	}
     
    public static void main(String[] args) throws IOException {
    new Lecture();
       }
    }
    J'ai mis "Lecture" en nom de classe, mais met ce que tu veux, mais je pense que l'appelée "main" créer une ambiguité...

  3. #3
    Membre régulier
    Profil pro
    Inscrit en
    Mai 2009
    Messages
    106
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mai 2009
    Messages : 106
    Points : 121
    Points
    121
    Par défaut
    Essaie sa :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    import java.io.*;
    import java.util.*; // pour StringTokeniser
     
    public class Main {
    	// Lecture dans un fichier texte
    	public static void main(String args[]) throws IOException {
     
    		String nomfich;
    		double x, y, z;
    		nomfich = "U.txt";
    		BufferedReader entree = new BufferedReader(new FileReader(nomfich));
    		System.out.println("Nombres contenus dans le fichier " + nomfich + ":");
    		String ligneLue;
    		while ((ligneLue = entree.readLine()) != null) {
    			StringTokenizer tok = new StringTokenizer(ligneLue, " ");
    			if(tok.countTokens() == 3)
    			{
    				x = Double.parseDouble(tok.nextToken().replace("(", ""));
    				y = Double.parseDouble(tok.nextToken());
    				z = Double.parseDouble(tok.nextToken().replace(")", ""));
    				System.out.println(x + " " + y + " " + z);
    			}
    		}
     
    		entree.close();
    		System.out.println("fin liste fichier" + nomfich);
    	}
    }
    Le probleme est les parentheses ( et ).
    L'exemple que je te donne est dans le cas specifiques de ton fichier car il supprime les parentheses ( et )

  4. #4
    Nouveau membre du Club
    Profil pro
    Inscrit en
    Avril 2010
    Messages
    55
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Avril 2010
    Messages : 55
    Points : 37
    Points
    37
    Par défaut
    merci bien je vais tester ça!!! Et ça marche!!

Discussions similaires

  1. Lecture dans un fichier texte.
    Par tazthedev dans le forum Delphi
    Réponses: 4
    Dernier message: 31/10/2006, 16h07
  2. Réponses: 4
    Dernier message: 23/03/2006, 17h12
  3. Réponses: 6
    Dernier message: 23/02/2006, 12h09
  4. [ASP] Lecture dans un fichier texte
    Par thoomis dans le forum ASP
    Réponses: 3
    Dernier message: 23/11/2005, 15h08
  5. [Débutant] Lecture dans un fichier texte
    Par babemagus dans le forum Entrée/Sortie
    Réponses: 10
    Dernier message: 07/07/2005, 12h17

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo