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

Langage Java Discussion :

probleme code moyenne


Sujet :

Langage Java

  1. #1
    Membre du Club
    Profil pro
    Inscrit en
    Mai 2008
    Messages
    213
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mai 2008
    Messages : 213
    Points : 43
    Points
    43
    Par défaut probleme code moyenne
    Bonjour, je me suis lancé dans un projet, avec un programme qui calculerai la moyenne. Mais j'ai un petit probleme, voici le code:

    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
    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
    import java.util.Scanner;
    import java.io.*;
     
     
    public class moyennePreFinal {
     
    	public static void ecriture(float[][] tab) throws IOException {
    		FileWriter monFichier = null;
    		BufferedWriter tampon = null;
    		try {
    			monFichier = new FileWriter("essai.txt", true);
    			tampon = new BufferedWriter(monFichier);
     
    			for(int i = 0 ; i < tab[0].length ; i++) {
    				tampon.write(tab[0][i] + "\n");
    				tampon.write(tab[1][i] + "\n");
    			}
     
    		}catch(IOException e) {
    			e.printStackTrace();
    			System.out.println("error fatal");
    		}
    		finally {
    			try {
    			tampon.flush();
    			tampon.close();
    			monFichier.close();
    			}catch(Exception e1) {
    				System.out.println("error fatal 2");
    				e1.printStackTrace();
    			}
    		}
     
    	}
     
    	public static float[][] lecture(float[][] tab) throws IOException {
    		tab = new float[2][1000];
    		FileReader monFichier = null;
    		BufferedReader tampon = null;
     
    		try {
    			monFichier = new FileReader("essai.txt");
    			tampon = new BufferedReader(monFichier);
    			int i = 0;
    			while(true) {
    		    String lecteurOctet = tampon.readLine();
    		if(lecteurOctet == null)
    				break;
     
    			float note = Integer.valueOf(lecteurOctet).floatValue();
     
    			tab[0][i] = note;
    			i++;
     
    			 lecteurOctet = tampon.readLine();
    			 if(lecteurOctet == null)
    					break;
    			float note1 = Integer.valueOf(lecteurOctet).floatValue();
     
    				tab[1][i] = note1;
    				i++;
    			}
    			for(int y = 0 ; y < tab[0].length ; y++) {
    				if(tab[1][y] == 0)
    					tab = new float[2][y];
    					break;
    			}
     
    		}catch(IOException e) {
    			e.printStackTrace();
    			System.out.println("error fatal");
    		}
    		finally {
    			try {
    			monFichier.close();
    			tampon.close();
    			}catch(Exception e1) {
    				System.out.println("error fatal 2");
    				e1.printStackTrace();
    			}
    		}
    		return tab;
     
    	}
     
     
     
     
    	public static float[] transformer(float[][] tab1, float[] tab2, int nombreDeNote) {
    		float resultat;
    		float dividende;
    		float diviseur;
     
    		for(int i = 0; i < nombreDeNote; i++) {
    			dividende = tab1[0][i];
    			diviseur = tab1[1][i];
    		resultat = dividende / diviseur*20;
    		tab2[i] = resultat;
    		}
    		return tab2;
    	}
     
    	public static void creationTableau(float[] tabis) {
    		int i = 0;
    		do {
    			System.out.println("" + tabis[i]);
    			i++;
    		}while(i < tabis.length);
    	}
     
     
    	public static void main(String[] args) throws IOException{
     
    	Scanner sc2 = new Scanner(System.in);
    	Scanner sc1 = new Scanner(System.in);
    	Scanner sc = new Scanner(System.in);
    	float moyenne;
    	float somme = 0;
    	System.out.println("Programme de moyenne");
    	System.out.println("choisis le nombre de note");
    	int str = sc.nextInt();
    	System.out.println("Ecrivez vos notes, appuyez sur entree et ecrivez sur combien elle est et rappuyé sur entrée: ");
    	float str1[][] = new float[2][str];
    	float tableauFinal[] = new float[str];
    	for(int i = 0; i < str; i++) {
    		System.out.println("note: ");
    		float str2 = sc1.nextFloat();
    	     str1[0][i] = str2;
    	     System.out.println("sur: ");
    	     float str3 = sc2.nextFloat();
    	     str1[1][i] = str3;
    	}
    	ecriture(str1);
    	lecture(str1);
    	transformer(str1, tableauFinal, str);
    	for(int i = 0; i < str; i++) {
    		somme = somme + tableauFinal[i];
     
    	}
    	System.out.println("vous avez rentré");
    	creationTableau(tableauFinal);
    	moyenne = somme/str;
    	System.out.println("votre moyenne est " + moyenne + " sur 20");
     
    	}
    }
    Quand je le run, il me met qu'il y a une erreure a la ligne de code suivante:

    float note = Integer.valueOf(lecteurOctet).floatValue();

    Quelqu'un sait il ce qui ne va pas?

    Merci d'avance

  2. #2
    Membre chevronné Avatar de guigui5931
    Profil pro
    Chef de projet NTIC
    Inscrit en
    Avril 2006
    Messages
    1 667
    Détails du profil
    Informations personnelles :
    Âge : 37
    Localisation : France, Nord (Nord Pas de Calais)

    Informations professionnelles :
    Activité : Chef de projet NTIC
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Avril 2006
    Messages : 1 667
    Points : 2 232
    Points
    2 232
    Par défaut
    C'est surement que tu saisie une chaine de caractère qui n'est pas un float.

  3. #3
    Membre du Club
    Profil pro
    Inscrit en
    Mai 2008
    Messages
    213
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mai 2008
    Messages : 213
    Points : 43
    Points
    43
    Par défaut
    Pourtant dans le fichier il n'y a que des nombres, c'est bizzard, surtout que quand je l'utilise seul, il marche tres bien.
    En tout cas merci de ta reponse tres rapide

  4. #4
    Membre chevronné Avatar de guigui5931
    Profil pro
    Chef de projet NTIC
    Inscrit en
    Avril 2006
    Messages
    1 667
    Détails du profil
    Informations personnelles :
    Âge : 37
    Localisation : France, Nord (Nord Pas de Calais)

    Informations professionnelles :
    Activité : Chef de projet NTIC
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Avril 2006
    Messages : 1 667
    Points : 2 232
    Points
    2 232
    Par défaut
    Tu n'a pas d'inversion entre le point et la virgule?

  5. #5
    Membre du Club
    Profil pro
    Inscrit en
    Mai 2008
    Messages
    213
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mai 2008
    Messages : 213
    Points : 43
    Points
    43
    Par défaut
    Non aucune, ce ne sont que des points.
    Il me met aussi une erreure dans

    lecture(str1);

    mais je pense aps que ca vienne de la

  6. #6
    Membre chevronné Avatar de guigui5931
    Profil pro
    Chef de projet NTIC
    Inscrit en
    Avril 2006
    Messages
    1 667
    Détails du profil
    Informations personnelles :
    Âge : 37
    Localisation : France, Nord (Nord Pas de Calais)

    Informations professionnelles :
    Activité : Chef de projet NTIC
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Avril 2006
    Messages : 1 667
    Points : 2 232
    Points
    2 232
    Par défaut
    Tu peux nous donner les traces d'erreurs que tu as ?

  7. #7
    Membre du Club
    Profil pro
    Inscrit en
    Mai 2008
    Messages
    213
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mai 2008
    Messages : 213
    Points : 43
    Points
    43
    Par défaut
    Tout de suite, les voila:

    Exception in thread "main" java.lang.NumberFormatException: For input string: "20.0"
    at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)
    at java.lang.Integer.parseInt(Integer.java:456)
    at java.lang.Integer.valueOf(Integer.java:553)
    at moyennePreFinal.lecture(moyennePreFinal.java:50)
    at moyennePreFinal.main(moyennePreFinal.java:134)

    Je vois vraiment pas...

    (mercid e m'aider)

  8. #8
    Membre chevronné Avatar de guigui5931
    Profil pro
    Chef de projet NTIC
    Inscrit en
    Avril 2006
    Messages
    1 667
    Détails du profil
    Informations personnelles :
    Âge : 37
    Localisation : France, Nord (Nord Pas de Calais)

    Informations professionnelles :
    Activité : Chef de projet NTIC
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Avril 2006
    Messages : 1 667
    Points : 2 232
    Points
    2 232
    Par défaut
    Tu fais un Integer.valueOf() sur un nombre qui n'est pas un Integer donc tu as un exception. Si tu veux direct un float tu peux faire
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
     
    Float.parseFloat(lecteurOctet);

  9. #9
    Membre du Club
    Profil pro
    Inscrit en
    Mai 2008
    Messages
    213
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mai 2008
    Messages : 213
    Points : 43
    Points
    43
    Par défaut
    Ok merci beaucoup

    J'ai fait note1 = Float.parseFloat(lecteurOctet);

    Mais ca ne arche toujours pas, il me met juste des nouveaux messages d'erreur

  10. #10
    Membre chevronné Avatar de guigui5931
    Profil pro
    Chef de projet NTIC
    Inscrit en
    Avril 2006
    Messages
    1 667
    Détails du profil
    Informations personnelles :
    Âge : 37
    Localisation : France, Nord (Nord Pas de Calais)

    Informations professionnelles :
    Activité : Chef de projet NTIC
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Avril 2006
    Messages : 1 667
    Points : 2 232
    Points
    2 232
    Par défaut
    Tu sais si tu ne nous donne pas les messages on ne peut pas vraiment t'aider.

  11. #11
    Membre du Club
    Profil pro
    Inscrit en
    Mai 2008
    Messages
    213
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mai 2008
    Messages : 213
    Points : 43
    Points
    43
    Par défaut
    oups desoler, voila les nouvaux messages:

    Exception in thread "main" java.lang.NumberFormatException: multiple points
    at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1084)
    at java.lang.Float.parseFloat(Float.java:422)
    at moyennePreFinal.lecture(moyennePreFinal.java:58)
    at moyennePreFinal.main(moyennePreFinal.java:134)


    Les voici

  12. #12
    Membre chevronné Avatar de guigui5931
    Profil pro
    Chef de projet NTIC
    Inscrit en
    Avril 2006
    Messages
    1 667
    Détails du profil
    Informations personnelles :
    Âge : 37
    Localisation : France, Nord (Nord Pas de Calais)

    Informations professionnelles :
    Activité : Chef de projet NTIC
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Avril 2006
    Messages : 1 667
    Points : 2 232
    Points
    2 232
    Par défaut
    Apparemment ta chaine ne correspond pas à un float. Essaye de l'afficher avant de la convertir pour voir ce que tu passe réellement à la méthode parseFloat.

  13. #13
    Membre du Club
    Profil pro
    Inscrit en
    Mai 2008
    Messages
    213
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mai 2008
    Messages : 213
    Points : 43
    Points
    43
    Par défaut
    Ok c'est bon a ce niveau la, merci beaucoup, en fait un nombre avait plusieurs points lol il a pas apprecié.
    Maintenant par contre(mais c'est moin grave) ma methode lecture ne marche pas, ou du moins ne stocke pas ds le tableau

    edit: Ok, en fait c'est pire que tout, personne ne me repond sur aucun fofo, j'en suis a me demander si ce n'est aps un bug de java...

    edit: Je sais d'ou ca vient, le pointeur est a la fin du fichier et je reinitialise le tableau. Y'aurait il une methode qui me permettrai de mettre le pointeur tout au debut du fichier?
    merci d'avance

    Encore merci

Discussions similaires

  1. probleme code vba ne s'execute pas apres instruction sql
    Par arnogef dans le forum Requêtes et SQL.
    Réponses: 9
    Dernier message: 29/12/2005, 12h34
  2. [TP] Problème code touche clavier
    Par phildeb dans le forum Turbo Pascal
    Réponses: 10
    Dernier message: 02/12/2005, 22h44
  3. Probleme code asm dans .c
    Par sorry60 dans le forum C
    Réponses: 5
    Dernier message: 18/04/2005, 13h15
  4. probleme de moyenne
    Par prez dans le forum Langage SQL
    Réponses: 2
    Dernier message: 25/08/2004, 17h38
  5. [langage] probleme code
    Par louisis dans le forum Langage
    Réponses: 5
    Dernier message: 30/06/2004, 17h43

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