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

avec Java Discussion :

Erreur avec if (choix = E)


Sujet :

avec Java

  1. #1
    Membre du Club
    Profil pro
    Inscrit en
    Novembre 2010
    Messages
    146
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Novembre 2010
    Messages : 146
    Points : 69
    Points
    69
    Par défaut Erreur avec if (choix = E)
    Bonsoir j'aimerais faire un test de saisie d'un caractère, pour lire au clavier j'utilise une class Lire.

    voici mon bout de script
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
     
    ....
    char choix;
    System.println("tapez la touche E ou la touche F");
    choix = Lire.c();
    if (choix == E) //eclipse m'indique E can't ge resolved
    {
    ....
    }
    pouquoi à la ligne if , j'ai une erreur de compilation ?

    voici la class Lire
    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
    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
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
     
    import java.io.*;
    public class Lire
    {
    public static String S() // Lire un String
    {
    String tmp = "";
    char C='\0';
    try {
             while ((C=(char) System.in.read()) !='\n')
             {
              if (C != '\r')  tmp = tmp+C;
     
             }
       }
     catch (IOException e)
            {
              System.out.println("Erreur de frappe");
              System.exit(0);
            }		
     return tmp;
    } // fin de S()
     
     public static byte b()  // Lire un entier byte
     {
      	 byte x=0;
     	 	try {
    			 x=Byte.parseByte(S());
    	  	 	}
     	 	catch (NumberFormatException e) {
              System.out.println("Format numérique incorrect");
              System.exit(0);
        }	
    	  return x ;
     }
     
     public static short s()  // Lire un entier short
     {
       	 short x=0;
     	 	try {
    			 x=Short.parseShort(S());
    	  	 	}
     	 	catch (NumberFormatException e) {
              System.out.println("Format numérique incorrect");
              System.exit(0);
        }	
    	  return x ;
     }
     
     public static int i()  // Lire un entier
     {		
     	 int x=0;
     	 	try {
    			 x=Integer.parseInt(S());
      	 	}
     	 	catch (NumberFormatException e) {
              System.out.println("Format numérique incorrect");
              System.exit(0);
        }	
    	  return x ;
     }
     
     public static long l()  // Lire un entier long
     {
     	 long x=0;
     	 	try {
    			 x=Integer.parseInt(S());
      	 	}
     	 	catch (NumberFormatException e) {
              System.out.println("Format numérique incorrect");
              System.exit(0);
        }	
    	  return x ;
     }
     
     public  static double d()  // Lire un double
     {
      	double x=0.0;
     	 	try {
    	 		x=Double.valueOf(S()).doubleValue();
     	 	}import java.io.*;
    public class Lire
    {
    public static String S() // Lire un String
    {
    String tmp = "";
    char C='\0';
    try {
             while ((C=(char) System.in.read()) !='\n')
             {
              if (C != '\r')  tmp = tmp+C;
     
             }
       }
     catch (IOException e)
            {
              System.out.println("Erreur de frappe");
              System.exit(0);
            }		
     return tmp;
    } // fin de S()
     
     public static byte b()  // Lire un entier byte
     {
      	 byte x=0;
     	 	try {
    			 x=Byte.parseByte(S());
    	  	 	}
     	 	catch (NumberFormatException e) {
              System.out.println("Format numérique incorrect");
              System.exit(0);
        }	
    	  return x ;
     }
     
     public static short s()  // Lire un entier short
     {
       	 short x=0;
     	 	try {
    			 x=Short.parseShort(S());
    	  	 	}
     	 	catch (NumberFormatException e) {
              System.out.println("Format numérique incorrect");
              System.exit(0);
        }	
    	  return x ;
     }
     
     public static int i()  // Lire un entier
     {		
     	 int x=0;
     	 	try {
    			 x=Integer.parseInt(S());
      	 	}
     	 	catch (NumberFormatException e) {
              System.out.println("Format numérique incorrect");
              System.exit(0);
        }	
    	  return x ;
     }
     
     public static long l()  // Lire un entier long
     {
     	 long x=0;
     	 	try {
    			 x=Integer.parseInt(S());
      	 	}
     	 	catch (NumberFormatException e) {
              System.out.println("Format numérique incorrect");
              System.exit(0);
        }	
    	  return x ;
     }
     
     public  static double d()  // Lire un double
     {
      	double x=0.0;
     	 	try {
    	 		x=Double.valueOf(S()).doubleValue();
     	 	}
     	 	catch (NumberFormatException e) {
              System.out.println("Format numérique incorrect");
              System.exit(0);
        }	
    	  return x ;
     }
     
     public  static float f()  // Lire un float
     {
       float x=0.0f;
     	try {
     		x=Double.valueOf(S()).floatValue();
        	}
     	catch (NumberFormatException e) 
     	{
              System.out.println("Format numérique incorrect");
              System.exit(0);
        }	
    	  return x ;
     }
     
     public  static char c()  // Lire un caractere
     {
      String tmp=S();
      if (tmp.length()==0)
    	  return '\n';
      else 
    		{
    		return tmp.charAt(0);
    		}
     }
    }
     
     	 	catch (NumberFormatException e) {
              System.out.println("Format numérique incorrect");
              System.exit(0);
        }	
    	  return x ;
     }
     
     public  static float f()  // Lire un float
     {
       float x=0.0f;
     	try {
     		x=Double.valueOf(S()).floatValue();
        	}
     	catch (NumberFormatException e) 
     	{
              System.out.println("Format numérique incorrect");
              System.exit(0);
        }	
    	  return x ;
     }
     
     public  static char c()  // Lire un caractere
     {
      String tmp=S();
      if (tmp.length()==0)
    	  return '\n';
      else 
    		{
    		return tmp.charAt(0);
    		}
     }
    }
    quelle est l'erreur que j'ai commit ?
    je vous remercie d'avance de vos réponse

  2. #2
    Expert éminent sénior
    Avatar de tchize_
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Avril 2007
    Messages
    25 481
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 44
    Localisation : Belgique

    Informations professionnelles :
    Activité : Ingénieur développement logiciels
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Avril 2007
    Messages : 25 481
    Points : 48 806
    Points
    48 806

  3. #3
    Membre du Club
    Profil pro
    Inscrit en
    Novembre 2010
    Messages
    146
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Novembre 2010
    Messages : 146
    Points : 69
    Points
    69
    Par défaut
    merci de ta réponse
    que suis je bête, j'ai encore oublié, c'est un caractère

    une autre question

    comment fait ton pour lire un double ?
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
     
    float argent;
    System.out.println("merci d'indiquer une somme");
    argent = Lire.d();
    Ici si je met Lire.f() cela fonctionne mais quand je met Lire.d() pour essayer cela ne fonctionne pas ? il faut changer le "float argent" ?

  4. #4
    Expert éminent sénior
    Avatar de tchize_
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Avril 2007
    Messages
    25 481
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 44
    Localisation : Belgique

    Informations professionnelles :
    Activité : Ingénieur développement logiciels
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Avril 2007
    Messages : 25 481
    Points : 48 806
    Points
    48 806
    Par défaut
    c'est quoi Lire.d() ?

  5. #5
    Membre du Club
    Profil pro
    Inscrit en
    Novembre 2010
    Messages
    146
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Novembre 2010
    Messages : 146
    Points : 69
    Points
    69
    Par défaut
    j'ai mis plus haut la class entière lire
    voici la partie d
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
     
     public  static double d()  // Lire un double
     {
      	double x=0.0;
     	 	try {
    	 		x=Double.valueOf(S()).doubleValue();
     	 	}
     	 	catch (NumberFormatException e) {
              System.out.println("Format numérique incorrect");
              System.exit(0);
        }	
    	  return x ;
     }

  6. #6
    Expert éminent sénior
    Avatar de tchize_
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Avril 2007
    Messages
    25 481
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 44
    Localisation : Belgique

    Informations professionnelles :
    Activité : Ingénieur développement logiciels
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Avril 2007
    Messages : 25 481
    Points : 48 806
    Points
    48 806
    Par défaut
    ça retourne un double. Pour le stocker dans un float, faut typecaster

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    monFloat = (float)monDouble

  7. #7
    Membre éclairé
    Profil pro
    Inscrit en
    Juillet 2007
    Messages
    802
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juillet 2007
    Messages : 802
    Points : 653
    Points
    653
    Par défaut
    Ce qui ne va pas, c'est que ta méthode d() retourne un double (64bits) et que ta variable argent est un float (32bits). Donc il y a un problème de casting.

    Il y a deux solutions :
    - Soit changer le type de la variable argent en double
    - Soit sous-caster le résultat de d() en float

    Pour cette dernière solution :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    float argent = (float) d();
    Le sous-casting présente néanmoins un inconvénient, c'est que le résultat peut être tronqué si la valeur retournée par d() est d'une précision supérieure à celle que peut admettre un float.

  8. #8
    Membre du Club
    Profil pro
    Inscrit en
    Novembre 2010
    Messages
    146
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Novembre 2010
    Messages : 146
    Points : 69
    Points
    69
    Par défaut
    merci beaucoup de ces réponses;
    donc pour que cela fonction il faut que je fasse
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
     
    double argent;
    argent = Lire.d();

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Réponses: 3
    Dernier message: 09/01/2008, 15h54
  2. Erreur avec procédure LockWorkStation ...
    Par simonseztech dans le forum API, COM et SDKs
    Réponses: 4
    Dernier message: 16/08/2004, 15h33
  3. [Débutant][Conception] Erreur avec une classe interne
    Par Devil Redneck dans le forum Général Java
    Réponses: 5
    Dernier message: 11/06/2004, 15h45
  4. Erreur avec les ADO
    Par megane dans le forum Bases de données
    Réponses: 7
    Dernier message: 08/03/2004, 21h37
  5. Erreur avec WM_COMMAND (BN_CLICKED)
    Par cyberlewis dans le forum Windows
    Réponses: 2
    Dernier message: 09/02/2004, 00h25

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