0

I'm trying to work on my homework for class, and I'm using a switch case to make a command line interface, but I'm having a problem with the part I'm using. When I execute the addCard command, it goes through fine, but then afterwards goes to the default section. When I try using any of the other sections, or use it without the try segment, it works and doesn't go to default. Any thoughts on how to repair this? Code below

public static void cmdLine(String Cmd) {
    switch(Cmd) {

        case "Help":
        case "?":
            System.out.println("Available Commands:\naddCard = Add a card!\ndeleteCard = Delete a Card!\nfindCard = Locate Card Number by Name!\nCard (Card Number) = Work with your card");
            break;
        case "addCard" :
            System.out.println ("Enter Account Name:");
            String cName = scan.nextLine();
            try {
                System.out.println ("Enter Account Start Balance:");
                int cBal = scan.nextInt();
                System.out.println ("Enter Account Number:");
                int cNum = scan.nextInt();
                PPArray.addCard(cName, cBal,cNum);
            } catch (InputMismatchException nfe) {
                System.out.println("InputMismatchException: Please try again with only the card number. (Ex. 12345");
                break;
            }
            break;
        case "deleteCard" :
            System.out.println("here we will have a command to remove the card from the array");
            break;
        case "Card" :
            System.out.println("This will lead to a new function to operate with said card");
            break;
        case "Exit":
        case "exit":
        case "Quit":
        case "quit":
            return;
        default :
            System.out.println("Invalid Command ('?' or 'Help' for commands)");
            System.out.println(PPArray.cardArray[1].name);
    }
    cmdLine(scan.nextLine());
}
  • Can you put break points at every line in the "addCard" section and tell us at which line it jumps to default? – ucsunil Oct 15 '14 at 04:59
  • can you rephrase the question? there seems to be some misunderstanding when you look at those two different answers! – Martin Frank Oct 15 '14 at 05:03

3 Answers3

2

When you run the try block in "addCard", you get input with int cNum = scan.nextInt();. Then, when you call cmdLine(scan.nextLine()); after the switch statement, it consumes the new line, which causes the default clause to be called.

You can prevent this by adding scan.nextLine() at the end of that try block.

    case "addCard" :
        System.out.println ("Enter Account Name:");
        String cName = scan.nextLine();
        try {
            System.out.println ("Enter Account Start Balance:");
            int cBal = scan.nextInt();
            System.out.println ("Enter Account Number:");
            int cNum = scan.nextInt();
            PPArray.addCard(cName, cBal,cNum);
            scan.nextLine();
        } catch (InputMismatchException nfe) {
            System.out.println("InputMismatchException: Please try again with only the card number. (Ex. 12345");
            break;
        }
        break;

BTW, I think it is bad coding to recursively call cmdLine(scan.nextLine()); in order to process the next input. I think a while loop would make more sense.

Eran
  • 359,724
  • 45
  • 626
  • 694
0

You have to actually exit the program if you want it to quit:

    case "Exit":
    case "exit":
    case "Quit":
    case "quit":
        return;

should be

    case "Exit":
    case "exit":
    case "Quit":
    case "quit":
        System.exit(0);

What is the next line?

cmdLine(scan.nextLine());

Is reading something that doesn't match any of the cases, your step debugger will show you where your logic error is. Step debugging is a much more valuable skill than System.out.println() and guessing.

  • this part i wanted, so that doing exit would exit out of the program itself. I'm just worried about the addCard case. it did it before i added the exit cases – Jack Boucher Oct 15 '14 at 04:59
-1

change this

case "Exit":
case "exit":
case "Quit":
case "quit":
     return;

to

case "Exit":
case "exit":
case "Quit":
case "quit":
      break;

EDIT:

One more little issue is cmdLine(scan.nextLine()); taking new line each time and causing default to execute. Better to use cmdLine(scan.next()); which do not accept multiple words or line.

Qadir Hussain
  • 1,248
  • 1
  • 9
  • 25
  • then it will just ask for the next line and not quit, this isn't correct at all. –  Oct 15 '14 at 05:02