Probleme java exception
Répondre à la discussion
Affichage des résultats 1 à 3 sur 3

Probleme java exception



  1. #1
    ser31

    Probleme java exception


    ------

    Bonjour,
    Je suis programmeur junior et j'essai de faire un code pour aditionner le total de lettre entré par une alphabet exemple:
    si je rentre ab le resultat doit m'afficher 1+2 = 3
    mon code est comme ceci :

    Code:
    final Map<Character, Integer> map;
            String str = "";
            System.out.println("Enter a word");
            str = new Scanner(System.in).next();
            map = new HashMap<>();
    
            map.put('a', 1);
            map.put('b', 2);
            map.put('c', 3);
            map.put('d', 4);
            map.put('e', 5);
            map.put('f', 6);
            map.put('g', 7);
            map.put('h', 8);
            map.put('i', 9);
            map.put('j', 10);
            map.put('k', 11);
            map.put('l', 12);
            map.put('m', 13);
            map.put('n', 14);
            map.put('o', 15);
            map.put('p', 16);
            map.put('q', 17);
            map.put('r', 18);
            map.put('s', 19);
            map.put('t', 20);
            map.put('u', 21);
            map.put('v', 22);
            map.put('w', 23);
            map.put('x', 24);
            map.put('y', 25);
            map.put('z', 26);
    
            for (final char c : str.toCharArray()) {
                final Integer val;
                val = map.get(c);
                if (val == null) {
                    System.out.print(" ERROR ");
                } else {
                    String Str = new String(val + " ");
                    for (String retval : Str.split(" ", c)) {
                        System.out.println(retval);
                       
                        try {
                            int i = Integer.parseInt(retval);
                            
                            int result =0;                        
                         
                           result += i;
                            System.out.println(result);
                        } catch (NumberFormatException e) {
                            System.out.println("ERROR !");
                        }
    quand j’exécute voici ce que ça m'affiche :
    Enter a word
    abc
    1
    1
    ERROR !
    2
    2
    ERROR !
    3
    3
    ERROR !

    autrement dit il passe directement à l'exception ..... quelqu'un pourrai m'aider svp!!
    Merci

    -----
    Dernière modification par JPL ; 17/07/2016 à 00h28. Motif: ajout de la balise Code pour garder l'indentation

  2. #2
    electron22

    Re : Probleme java exception

    Bonjour,
    L'erreur vient du fait que tu veux convertir un String vide en un entier, parseInt retourne une exception. En effet, quand tu as crée le Str plus un espace (pour une raison que j'ignore d'ailleurs).
    Code:
    String Str = new String(val + " ");
    , ensuite tu viens accéder à la 2ème case qui est vide puis la convertir...
    Si j'ai bien compris ton algo, tu pourrais faire mieux :
    Code:
        	
    int resul = 0;
            for (final char c : str.toCharArray()) 
            {
                final Integer val;
                val = map.get(c);
                if (val == null) 
                {
                    System.out.print(" ERROR ");
                } 
                else 
                {
                	resul  = resul + val; // val est déjà la bonne valeur qu'il te faut, il suffit d'additionner avec les précédents résultats
                }
            }
            System.out.print(resul);
    	}
    }

  3. #3
    ser31

    Re : Probleme java exception

    Bonjour
    merci pour votre réponse
    l'espace dans mon string est pour séparer entre chaque résultat sinon tout sera collé mais finalement j'aurai compliqué mon code pour rien alors que c'était simple.
    Merci beaucoup!

Discussions similaires

  1. probleme sur java
    Par invite9da73615 dans le forum Programmation et langages, Algorithmique
    Réponses: 7
    Dernier message: 13/06/2012, 13h08
  2. Problème Java
    Par invitee32ad308 dans le forum Programmation et langages, Algorithmique
    Réponses: 32
    Dernier message: 23/09/2011, 18h36
  3. Probleme java
    Par BIGFOOT176 dans le forum Logiciel - Software - Open Source
    Réponses: 1
    Dernier message: 09/11/2009, 17h36
  4. probléme Java et IE7 ?
    Par mistigri78 dans le forum Logiciel - Software - Open Source
    Réponses: 0
    Dernier message: 13/06/2009, 11h23
  5. [C++] Probleme d'exception / C++ Builder
    Par JP dans le forum Logiciel - Software - Open Source
    Réponses: 8
    Dernier message: 12/09/2003, 17h38