Possible Duplicate:
Switch Statement with Strings in Java
Ok, basically i want to know if this is possible and how to do it. I have a menu and i want this switch statement to recognize chars or a string that was entered by the user.
I have a little menu program that I am writing:
import java.util.*;
import java.io.*;
import java.io.File;
import java.io.FileReader;
public class Personnel
{
// Instance Variables
private static String empName;
private static double wage;
public static void main(String[] args) throws Exception
{
clearScreen();
question();
printMenu();
exit();
}
public static void clearScreen()
{
System.out.println("\u001b[H\u001b[2J");
}
private static void exit()
{
System.exit(0);
}
private static void printMenu()
{
System.out.println("\t------------------------------------");
System.out.println("\t|Commands: n - New employee |");
System.out.println("\t| c - Compute paychecks |");
System.out.println("\t| r - Raise wages |");
System.out.println("\t| p - Print records |");
System.out.println("\t| d - Download data |");
System.out.println("\t| u - Upload data |");
System.out.println("\t| q - Quit |");
System.out.println("\t------------------------------------");
System.out.println("");
}
private static void question()
{
System.out.println("Enter command: ");
Scanner q = new Scanner(System.in);
// String input = q.nextLine();
switch (q.nextLine())
{
case "n":
System.out.println("Enter the name of new employee: ");
Scanner stdin = new Scanner(System.in);
System.out.println("Hourly (h) or salaried (s): ");
Scanner stdin2 = new Scanner(System.in);
if (stdin2.equals("h"))
{
System.out.println("Enter hourly wage: ");
Scanner stdin3 = new Scanner(System.in);
}
else
{
System.out.println("Enter annual salary: ");
Scanner stdin4 = new Scanner(System.in);
}
break;
/*
* case 9:
* System.out.println ("Please proceed.");
* new InputMenu();
* break;
* default:
* System.err.println ("Unrecognized option" ); break;
*/
}
}
}
What i am trying to do is make this menu work so that you can type in a command then i will write methods to run depending on what command was typed in. Is switch statement the best way to do this? After they enter in the info i need it to "break" back to the main menu. And anytime during the entire program, if they enter q then the program exits. I'm not sure the best way to go about doing this.
Thanks!