2015-08-02 4 views
0

J'ai écrit le morceau de code Java ci-dessous. Lorsque vous l'exécutez et que vous tapez une valeur quelconque (soit une valeur définie, par exemple latte, soit toute autre valeur, par exemple un entier az), j'obtiens une exception InputMismatchException.InputMismatchException: Chaîne non reconnue

Autant que j'ai pu trouver des réponses, cette exception signifie que le type d'entrée ne correspond pas au type attendu. Qu'est-ce qui me manque, pourquoi le code ne reconnaît-il pas une entrée String? Merci pour le recul.

Cheers, Gabor

package Lesson1; 

import java.util.Scanner; 

public class Coffee { 

    public static void main(String[] args) { 

     //I define the type of coffees as Strings, plus the order as String as well 
     String espresso = "espresso"; 
     String americano = "americano"; 
     String cappuccino = "cappuccino"; 
     String latte = "latte"; 
     String order = new String(); 

     //I ask the user for their input 
     Scanner choice = new Scanner(System.in); 
     System.out.println("What kind of coffee would you like? We have: espresso, americano, cappuccino and latte"); 

     //depending on the user's choice, the corresponding name is displayed; if any other string is entered, the else clause is displayed 
     if (order.equals(choice.next(espresso))) { 
      System.out.println("Your order: " + espresso); 

     } else if (order.equals(choice.next(americano))) { 
      System.out.println("Your order: " + americano); 

     } else if (order.equals(choice.next(cappuccino))) { 
      System.out.println("Your order: " + cappuccino); 

     } else if (order.equals(choice.next(latte))) { 
      System.out.println("Your order: " + latte); 

     } else { 
      System.out.println("Unfortunately we can't serve you. Have a nice day!"); 
     } 

    } 

} 



Exception in thread "main" java.util.InputMismatchException 
    at java.util.Scanner.throwFor(Unknown Source) 
    at java.util.Scanner.next(Unknown Source) 
    at java.util.Scanner.next(Unknown Source) 
    at Lesson1.Coffee.main(Coffee.java:22) 

Répondre

0

Je pense que vous utilisez le scanner incorrect. Essayer d'utiliser la méthode next() sans paramètre pour obtenir l'entrée de l'utilisateur, et ne l'appeler qu'une fois (au lieu de l'intérieur de chaque branche else). Comme ceci:

package com.company; 

import java.util.Scanner; 

public class Coffee { 
    public static void main(String[] args) { 

     //I define the type of coffees as Strings, plus the order as String as well 
     String espresso = "espresso"; 
     String americano = "americano"; 
     String cappuccino = "cappuccino"; 
     String latte = "latte"; 

     //I ask the user for their input 
     Scanner choice = new Scanner(System.in); 
     System.out.println("What kind of coffee would you like? We have: espresso, americano, cappuccino and latte"); 

     //depending on the user's choice, the corresponding name is displayed; if any other string is entered, the else clause is displayed 

     String order = choice.next(); 
     if (order.equals(espresso)) { 
      System.out.println("Your order: " + espresso); 

     } else if (order.equals(americano)) { 
      System.out.println("Your order: " + americano); 

     } else if (order.equals(cappuccino)) { 
      System.out.println("Your order: " + cappuccino); 

     } else if (order.equals(latte)) { 
      System.out.println("Your order: " + latte); 

     } else { 
      System.out.println("Unfortunately we can't serve you. Have a nice day!"); 
     } 

    } 
} 
1

Vous écrivez une fois en entrée par défaut, mais vous essayez de lire plusieurs fois à l'aide choice.next (..).

Une solution consiste à affecter votre choix dans une chaîne avant l'instruction if-else et à la vérifier à l'aide de equalsIgnoreCase.

//I ask the user for their input 
    Scanner choice = new Scanner(System.in); 
    System.out.println("What kind of coffee would you like? We have: espresso, americano, cappuccino and latte"); 

    String picked = choice.next(); 
    //depending on the user's choice, the corresponding name is displayed; if any other string is entered, the else clause is displayed 
    if (picked.equalsIgnoreCase(espresso)) { 
     System.out.println("Your order: " + espresso); 

    } else if (picked.equalsIgnoreCase(americano)) { 
     System.out.println("Your order: " + americano); 

    } else if (picked.equalsIgnoreCase(cappuccino)) { 
     System.out.println("Your order: " + cappuccino); 

    } else if (picked.equalsIgnoreCase(latte)) { 
     System.out.println("Your order: " + latte); 

    } else { 
     System.out.println("Unfortunately we can't serve you. Have a nice day!"); 
    }