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

AWT/Swing Java Discussion :

incompatibilité entre String et int


Sujet :

AWT/Swing Java

  1. #1
    Membre du Club
    Femme Profil pro
    Ingénieur en Télécommunication
    Inscrit en
    Octobre 2017
    Messages
    121
    Détails du profil
    Informations personnelles :
    Sexe : Femme
    Âge : 30
    Localisation : Algérie

    Informations professionnelles :
    Activité : Ingénieur en Télécommunication
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Octobre 2017
    Messages : 121
    Points : 63
    Points
    63
    Par défaut incompatibilité entre String et int
    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
     
    statement= connection.prepareStatement("DELETE  FROM Versement WHERE Nom=?AND Prenom=?AND Date=? AND Somme_versee=? AND id_verse=?" );
    					statement.setString(1,(String) comboBox_1.getSelectedItem() );
    					statement.setString(2, comboBox_2.getSelectedItem().toString());
    					statement.setString(3, date.getText());
    					statement.setString(4, verse.getText());
    					statement.setString(5,  comboBox_4.getSelectedItem().toString());
     
    					statement.executeUpdate();
    					if((int)(comboBox_4.getSelectedItem())==2) {
     
    					statement= connection.prepareStatement(" UPDATE Client SET Verse_restant=(SELECT (Prix_du_loge - SUM(Somme_versee)) FROM Versement WHERE Nom=? AND Prenom=?), Verse_total=(SELECT (SUM(Somme_versee)) FROM Versement WHERE Nom=? AND Prenom=? )  WHERE Nom=? AND Prenom=?");
    					statement.setString(1,(String) comboBox_1.getSelectedItem() );
    					statement.setString(2, comboBox_2.getSelectedItem().toString());
    					statement.setString(3,(String) comboBox_1.getSelectedItem() );
    					statement.setString(4, comboBox_2.getSelectedItem().toString());
    					statement.setString(5,(String) comboBox_1.getSelectedItem() );
    					statement.setString(6, comboBox_2.getSelectedItem().toString());
    					statement.executeUpdate();
    					JOptionPane.showMessageDialog(null, " Versement Annulé ");
    					statement.close();
    					statement.close();
    					statement.close();
    					statement.close();
    					}


    erreur: java.lang.String cannot be cast to java.lang.Integer dans la ligne : if((int)(comboBox_4.getSelectedItem())==2)

  2. #2
    Membre actif
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Février 2006
    Messages
    70
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 37
    Localisation : France, Alpes Maritimes (Provence Alpes Côte d'Azur)

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : High Tech - Électronique et micro-électronique

    Informations forums :
    Inscription : Février 2006
    Messages : 70
    Points : 218
    Points
    218
    Par défaut
    Visiblement getSelectedItem retourne une chaine de caractère String.

    Si le contenu de la String représente un nombre (et à cette seule condition, autrement un exception de type NumberFormatException sera levée), on peut le parser pour obtenir un Integer

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    int i = Integer.parseInt("123"); // i vaut 123

    Ta condition devient alors

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    if(  Integer.parseInt(comboBox_4.getSelectedItem()) == 2) {

  3. #3
    Modérateur
    Avatar de joel.drigo
    Homme Profil pro
    Ingénieur R&D - Développeur Java
    Inscrit en
    Septembre 2009
    Messages
    12 430
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 54
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Ingénieur R&D - Développeur Java
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Septembre 2009
    Messages : 12 430
    Points : 29 131
    Points
    29 131
    Billets dans le blog
    2
    Par défaut
    Ou alors c'est la position de l'item sélectionné que tu veux et non l'item sélectionné lui-même (la valeur visible dans la combo) : c'est par la méthode JComBox.getSelectedIndex() qu'on y accède (et qui retourne un int, elle).
    L'expression "ça marche pas" ne veut rien dire. Indiquez l'erreur, et/ou les comportements attendus et obtenus, et donnez un Exemple Complet Minimal qui permet de reproduire le problème.
    La plupart des réponses à vos questions sont déjà dans les FAQs ou les Tutoriels, ou peut-être dans une autre discussion : utilisez la recherche interne.
    Des questions sur Java : consultez le Forum Java. Des questions sur l'EDI Eclipse ou la plateforme Eclipse RCP : consultez le Forum Eclipse.
    Une question correctement posée et rédigée et vous aurez plus de chances de réponses adaptées et rapides.
    N'oubliez pas de mettre vos extraits de code entre balises CODE (Voir Mode d'emploi de l'éditeur de messages).
    Nouveau sur le forum ? Consultez Les Règles du Club.

  4. #4
    Membre du Club
    Femme Profil pro
    Ingénieur en Télécommunication
    Inscrit en
    Octobre 2017
    Messages
    121
    Détails du profil
    Informations personnelles :
    Sexe : Femme
    Âge : 30
    Localisation : Algérie

    Informations professionnelles :
    Activité : Ingénieur en Télécommunication
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Octobre 2017
    Messages : 121
    Points : 63
    Points
    63
    Par défaut
    je veux retourner la valeur et non pas son index (les valeurs sont 2, 3, 4)
    j'ai déja essayé avec : if(Integer.parseInt(comboBox_4.getSelectedItem()) == 2) et ça me donne cette erreur
    The method parseInt(String) in the type Integer is not applicable for the arguments (Object)

  5. #5
    Membre du Club
    Femme Profil pro
    Ingénieur en Télécommunication
    Inscrit en
    Octobre 2017
    Messages
    121
    Détails du profil
    Informations personnelles :
    Sexe : Femme
    Âge : 30
    Localisation : Algérie

    Informations professionnelles :
    Activité : Ingénieur en Télécommunication
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Octobre 2017
    Messages : 121
    Points : 63
    Points
    63
    Par défaut
    j'ai trouvé la solution,
    Merci beaucoup
    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
     
    statement= connection.prepareStatement("DELETE  FROM Versement WHERE Nom=?AND Prenom=?AND Date=? AND Somme_versee=? AND id_verse=?" );
    					statement.setString(1,(String) comboBox_1.getSelectedItem() );
    					statement.setString(2, comboBox_2.getSelectedItem().toString());
    					statement.setString(3, date.getText());
    					statement.setString(4, verse.getText());
    					statement.setString(5,(comboBox_4.getSelectedItem().toString()));
     
    					statement.executeUpdate();
    					if(Integer.valueOf((String) comboBox_4.getSelectedItem())  == (2) ) {
    					statement= connection.prepareStatement(" UPDATE Client SET Verse_restant=(SELECT (Prix_du_loge - SUM(Somme_versee)) FROM Versement WHERE Nom=? AND Prenom=?), Verse_total=(SELECT (SUM(Somme_versee)) FROM Versement WHERE Nom=? AND Prenom=? ) , Verse_2=0 WHERE Nom=? AND Prenom=?");
    					statement.setString(1,(String) comboBox_1.getSelectedItem() );
    					statement.setString(2, comboBox_2.getSelectedItem().toString());
    					statement.setString(3,(String) comboBox_1.getSelectedItem() );
    					statement.setString(4, comboBox_2.getSelectedItem().toString());
    					statement.setString(5,(String) comboBox_1.getSelectedItem() );
    					statement.setString(6, comboBox_2.getSelectedItem().toString());
    					statement.executeUpdate();
    					JOptionPane.showMessageDialog(null, " Versement Annulé ");
    					statement.close();
    					statement.close();
    					statement.close();
    					statement.close();
    					} 
    					if(Integer.valueOf((String) comboBox_4.getSelectedItem())  == (3) ) {
     
     
     
     
    											statement= connection.prepareStatement(" UPDATE Client SET Verse_restant=(SELECT (Prix_du_loge - SUM(Somme_versee)) FROM Versement WHERE Nom=? AND Prenom=?), Verse_total=(SELECT (SUM(Somme_versee)) FROM Versement WHERE Nom=? AND Prenom=? ) , Verse_3=0 WHERE Nom=? AND Prenom=?");
    						statement.setString(1,(String) comboBox_1.getSelectedItem() );
    						statement.setString(2, comboBox_2.getSelectedItem().toString());
    						statement.setString(3,(String) comboBox_1.getSelectedItem() );
    						statement.setString(4, comboBox_2.getSelectedItem().toString());
    						statement.setString(5,(String) comboBox_1.getSelectedItem() );
    						statement.setString(6, comboBox_2.getSelectedItem().toString());
    						statement.executeUpdate();
    						JOptionPane.showMessageDialog(null, " Versement Annulé ");
    						statement.close();
    						statement.close();
    						statement.close();
    						statement.close();
    						}

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

Discussions similaires

  1. Différence entre String et Int
    Par Alchimist dans le forum Langage
    Réponses: 6
    Dernier message: 05/04/2006, 11h00
  2. [débutant] String ou int
    Par pingoui dans le forum Langage
    Réponses: 42
    Dernier message: 16/09/2004, 09h16
  3. Réponses: 2
    Dernier message: 21/06/2004, 15h55
  4. [FLASH MX 2004] conversion string en int.
    Par calfater dans le forum Flash
    Réponses: 3
    Dernier message: 26/05/2004, 15h00
  5. Réponses: 2
    Dernier message: 25/05/2004, 11h40

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