0

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.

6
  • So what do you see? It doesn't help that we can't run the code you've provided because it isn't a short but complete program. (It's also not clear why you're constructing a Scanner wrapping System.in and then reading from it directly...) Commented Oct 2, 2015 at 14:52
  • 2
    Don't use 'while (exit == false) {' ...instead use 'while (!exit){' Commented Oct 2, 2015 at 14:52
  • Do man.infoMenu() and airmon.airMonMenu() contain print statements? Can you edit so we can see the source for these methods? Commented Oct 2, 2015 at 14:54
  • You never try to read a character in your loop; the mishap is therefore quite obvious, right? Commented Oct 2, 2015 at 14:54
  • When I run this as shown, I receive the message "Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - ". Commented Oct 2, 2015 at 14:54

4 Answers 4

4

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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;

        }

     }
   }

Comments

0

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.

Comments

0

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;

  }
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.