mercredi 29 avril 2020

If-else with print statement is skipping scanner

I have this code here but the if-else statement always skips over my second scanner. What am I doing wrong here? I got Login Debit in my console.

public static Scanner keyboard = new Scanner(System.in);
public static void main(String[] args) {
    String returnStatement = "";
    System.out.println("Welcome to Hollow gas station! Please let me know whether you are using a debit card or a credit card:\n"
            + "(1) Debit card \n(2) Credit card\n> ");
    int cardType = keyboard.nextInt();
    System.out.println("Awesome! Please enter your card number here: ");

    String cardNum = keyboard.nextLine();
    keyboard.next();
    if(cardType == 1) {
        returnStatement = String.format("Login\t%s\t%s", "Debit", cardNum);
    }
    else if(cardType == 2) {
        returnStatement = String.format("Login\t%s\t%s", "Credit", cardNum);
    }
    else {
        returnStatement = "Error";
    }
    System.out.println(returnStatement);
}

Aucun commentaire:

Enregistrer un commentaire