NewToThis
NewToThis

Reputation: 413

Java switch case not running

The switch case is not printing output, nor is it running.

package aircrack.ng;

import java.util.Scanner;

public class main {

    public static void main(String[] args) {

        Scanner in = new Scanner(System.in);
        infoMenu man = new infoMenu();
        airMonMenu airmon = new airMonMenu();
        boolean exit = false;
        char optSelect = (char) System.in.read();

        while (exit == false) {

            System.out.println("\nPlease select which menu you'd like to      open...");
            System.out.println("To view information about the tools included type: 'i' ");
            System.out.println("To enter the airmon menu type: 'a'");
            System.out.println("To exit simply type: 'e'\n");

            switch (optSelect) {
            case 'i':
                man.infoMenu();
                break;
            case 'a':
                airmon.airMonMenu();
                break;
            case 'e':
                exit = true;

            }

        }
   }

}

The ultimate goal here is to create a sort of menu to prompt for user input, then navigate to whichever menu the user selects. I want it all to continue to loop until the user inputs 'e', in which case it would break the loop.

Upvotes: 0

Views: 2126

Answers (4)

Sajan Chandran
Sajan Chandran

Reputation: 11487

In addition to the solution suggested, instead of using System.in.read() which reads exactly one byte at a time but your direct input can be more than one byte. So instead of using read method, you can use the Scanner which you have initialized but not used. For more information on System.in.read System.in.read() method

http://docs.oracle.com/javase/7/docs/api/java/io/InputStream.html#read%28%29 You can do something like

Scanner in = new Scanner(System.in);
boolean exit = false;

while (!exit) {

  System.out.println("\nPlease select which menu you'd like to      open...");
  System.out.println("To view information about the tools included type: 'i' ");
  System.out.println("To enter the airmon menu type: 'a'");
  System.out.println("To exit simply type: 'e'\n");

  char optSelect = in.next().charAt(0);
  switch (optSelect) {
    case 'i':
        System.out.println("i");
        break;
    case 'a':
        System.out.println("a");
        break;
    case 'e':
        System.out.println("e");
        exit = true;

  }
}

Upvotes: 0

Lexicographical
Lexicographical

Reputation: 501

You're retrieving user input before you actually enter the while loop. You should call the user input inside the loop after you display the instructions.

    package aircrack.ng;

    import java.util.Scanner;

    public class main {

      public static void main(String[] args) {

        Scanner in = new Scanner(System.in);
        infoMenu man = new infoMenu();
        airMonMenu airmon = new airMonMenu();
        boolean exit = false;


        while (!exit) {

          System.out.println("\nPlease select which menu you'd like to      open...");
          System.out.println("To view information about the tools included type: 'i' ");
          System.out.println("To enter the airmon menu type: 'a'");
          System.out.println("To exit simply type: 'e'\n");
          char optSelect = (char) System.in.read();

          switch (optSelect) {
            case 'i':
                man.infoMenu();
                break;
            case 'a':
                airmon.airMonMenu();
                break;
            case 'e':
                exit = true;

        }

     }
   }

}

On a side note, I highly recommend following Java naming conventions and renaming your classes to start with a capital letter.

Upvotes: 0

NightsWatch
NightsWatch

Reputation: 467

I have updated your main method as below; Your input collection happens outside the while loop, without any indicator to user to enter data hence you are not seeing anything eventhough program waits for user to enter. Also if the "i" then the while will enter never ending loop

public static void main(String[] args) throws IOException {

        Scanner in = new Scanner(System.in);
        boolean exit = false;

        while (exit == false) {
            System.out.println("Enter char---");
            char optSelect = (char) System.in.read();
          System.out.println("\nPlease select which menu you'd like to      open...");
          System.out.println("To view information about the tools included type: 'i' ");
          System.out.println("To enter the airmon menu type: 'a'");
          System.out.println("To exit simply type: 'e'\n");

          switch (optSelect) {
            case 'i':
                 man.infoMenu();
                break;
            case 'a':
                airmon.airMonMenu();
                break;
            case 'e':
                exit = true;

        }

     }
   }

Upvotes: 1

Tunaki
Tunaki

Reputation: 137064

The line when you are retrieving user input is mis-placed. It should be located after you print the instructions:

public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    infoMenu man = new infoMenu();
    airMonMenu airmon = new airMonMenu();
    boolean exit = false;
    while (!exit) {
        System.out.println("\nPlease select which menu you'd like to      open...");
        System.out.println("To view information about the tools included type: 'i' ");
        System.out.println("To enter the airmon menu type: 'a'");
        System.out.println("To exit simply type: 'e'\n");
        char optSelect = (char) System.in.read(); //should be here
        switch (optSelect) {
        case 'i':
            man.infoMenu();
            break;
        case 'a':
            airmon.airMonMenu();
            break;
        case 'e':
            exit = true;

    }

}

Note that I changed your while condition to read while (!exit).

You should also consider adding a default clause to the switch statement to handle the case when the user types another character.

Upvotes: 4

Related Questions