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 :

convertir du JTextField.gettext() ent int


Sujet :

Langage Java

  1. #1
    Membre régulier
    Profil pro
    Inscrit en
    Novembre 2005
    Messages
    200
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Novembre 2005
    Messages : 200
    Points : 104
    Points
    104
    Par défaut convertir du JTextField.gettext() ent int
    Bonjour,
    J'ai un champs de text: JTextField
    et je veux le convertir en int ou Integer
    Voici le code
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
     
    String s = new String();
    s =(String) fenetreP.fenetreConteneur.fenetreSecondaire1.kValue.getText();	
    Integer nbClusters = strToInt(s);
    Voici la méthode strToInt:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    public static int strToInt(String s)
    {
    Integer ger = new Integer(s);
    int i = ger.intValue();
    return i;
    }
    L'erreur est dans la ligne
    s =(String) fenetreP.fenetreConteneur.fenetreSecondaire1.kValue.getText();
    Et voici le message d'erreur
    Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: "k"
    at java.lang.NumberFormatException.forInputString(Unknown Source)
    at java.lang.Integer.parseInt(Unknown Source)
    at java.lang.Integer.<init>(Unknown Source)
    at IFCLUSTFRAME.strToInt(IFCLUSTFRAME.java:404)
    at IFCLUSTFRAME$6.actionPerformed(IFCLUSTFRAME.java:479)
    at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
    at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)Ce n'est pas un entier

    at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
    at java.awt.Component.processMouseEvent(Unknown Source)
    at javax.swing.JComponent.processMouseEvent(Unknown Source)
    at java.awt.Component.processEvent(Unknown Source)
    at java.awt.Container.processEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Window.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)
    "le professionnel et le chercheur sont comme le fleuve et l'océan"

  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
    Par défaut
    Pour convertir une string en nombre:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
     
    String s = .... // récupérer le textfield
    int i = 0;
    try {
        i = Integer.parseInt(s);
    } catch (NumberFormatException e){
        // n'est pas un nombre, gérer ce cas
    }

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

Discussions similaires

  1. [JTextField] getText renvoie toujours la même valeur
    Par Sufflope dans le forum Composants
    Réponses: 4
    Dernier message: 13/01/2007, 01h12
  2. Convertir une chaine quelconque en int
    Par PaladinFr dans le forum Collection et Stream
    Réponses: 6
    Dernier message: 25/11/2006, 14h48
  3. Convertir un JTextField en String
    Par remooz dans le forum Composants
    Réponses: 22
    Dernier message: 14/06/2006, 12h33
  4. [XSLT]Convertir une chaîne en float, int etc ... avec XSL
    Par wozzz dans le forum XSL/XSLT/XPATH
    Réponses: 3
    Dernier message: 19/05/2006, 10h12
  5. [C#] convertir un nombre hexa en int
    Par linux dans le forum C#
    Réponses: 5
    Dernier message: 08/12/2005, 16h38

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