2014-10-20 66 views
1

非常感謝您的幫助!如何在掃描器上使用enum類型

使用帶枚舉類型的掃描器時出現錯誤。但是我沒有被允許在這個任務中使用Buffer(InputStreamReader)。圍繞它的最佳工作是什麼?

我收到以下錯誤:

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
Cannot switch on a value of type String for source level below 1.7. Only convertible int values or enum variables are permitted 

at ui.Application.runCLI(Application.java:33) 
at ui.Application.main(Application.java:13) 

代碼:

package ui; 

進口java.util.Scanner的;

公共類應用{

static String command; 

public enum Command { 
    CONNECT, DISCONNECT, SEND, LOGLEVEL, HELP, QUIT, EXIT 
} 

private static void run(Scanner sc) { 
    // String command; // ready for the input 

    boolean done = false; // ready for the menu loop 
    while (!done) { // keep on until done 

     System.out 
       .println("Milestone1: Connection and interation with TCP server"); 
     System.out 
       .println("-------------------Please select on of the commandso-------------------------------------"); 
     System.out.println("connect"); 
     System.out.println("disconnect"); 
     System.out.println("send"); 
     System.out.println("logLevel"); 
     System.out.println("help"); 
     System.out.println("quit"); 
     System.out.println("exit"); 

     command = sc.nextLine(); // take user input   
     Command cmd=null; 
     try{ 
     cmd=Command.valueOf(command.toUpperCase()); 
     } 
     catch (IllegalArgumentException e){ 
      System.out.println("Invalid input"); 
      return; 
     } 
     switch (cmd) { 

     case EXIT: // exit menu 
      done = true;// condition for breaking the loop 
      break; 

     case CONNECT: 

      System.out.print(" IP adress: "); 

      try { 
       String userInput = sc.toString(); // user Input 

       System.out.println(" Port: "); 

       int userInput1 = sc.nextInt();// user Input 

       if (userInput1 >= 0) { 

        System.out.println(" EcoClient>" + " " + command + " " 
          + userInput + " " + userInput1); 
       } else { 
        System.out 
          .println("Entered value for Port is negative number or IP adress length < 7 || > 15, not in n.n.n.n format "); 
       } 

      } 

      catch (Exception e) {// throw exception in case of illogical 
            // input 
       System.out.println("\nBad input, please try again "); 
       sc.nextLine(); // remove leftover "\n" 
      } 

      break; 

     case DISCONNECT: 

      System.out.println(" EcoClient>" + " " + command); 

      break; 

     case SEND: 

      System.out 
        .println("Please enter " + " Hello World " + "phrase"); 
      try { 
       String userInput = sc.toString(); // user Input 
       System.out.println(" EcoClient>" + " " + command + " " 
         + userInput); 

      } 

      catch (Exception e) {// throw exception in case of illogical 
            // input 

       System.out.println("\nBad input, please try again "); 
       sc.nextLine(); // remove leftover "\n" 
      } 
      break; 

     case LOGLEVEL: 
      try { 
       System.out.println(" EcoClient>" + " " + command + "< " 
         + "current log status" + " >"); 
      } 

      catch (Exception e) {// throw exception in case of illogical 
            // input 

       System.out.println("\nBad input, please try again "); 
       sc.nextLine(); // remove leftover "\n" 

      } 
      break; 

     case HELP: 
      try { 
       System.out 
         .println("Following set of commands provide following functionalities:" 
           + " connect: establishes connection to the eco server " 
           + "disconnect: disconnects from the server and receives confirmation message " 
           + "send: sends the message to the server " 
           + "logLevel: prints out current log status" 
           + "quit: quits and notifies user about program shut down " 
           + "exit: cancel the input"); 

      } 

      catch (Exception e) {// throw exception in case of illogical 
            // input 

       System.out.println("\nBad input, please try again "); 
       sc.nextLine(); // remove leftover "\n" 
      } 
      break; 

     case QUIT: 

      try { 
       System.out.println(" EcoClient> " + command); 

      } 

      catch (Exception e) {// throw exception in case of illogical 
            // input 

       System.out.println("\nBad input, please try again "); 
       sc.nextLine(); // remove leftover "\n" 

      } 
      break; 

     default: 
      System.out.println("Does not recognise " 
        + "the input, pl. try again"); 

     } 

    } 
} 

public static void main(String[] args) { 

    Scanner sc = new Scanner(System.in);// will take user input 

    run(sc); 

} 

}

+0

不要忘了把這個詞'static'在main'的'的聲明 - 而且,無論是調用'run'之前實例化類或聲明'run'也是靜態的。 – 2014-10-20 02:18:22

+0

但是,謝謝你添加靜態並不能解決問題 – 2014-10-20 20:46:04

+0

stacktrace提到'runCLI'而不是上面的'run'? – 2014-10-20 21:02:24

回答

2

sc.nextLine()返回一個字符串。您需要使用靜態方法Command.valueOf(String)將其轉換爲Command(其中您使用的是switch)的實例,該靜態方法Command.valueOf(String)解析字符串並返回匹配的Command實例。

這裏的基礎知識:

command = sc.nextLine(); // take user input 
Command cmd = null; 
try { 
    cmd = Command.valueOf(command.toUpperCase()); 
} catch (IllegalArgumentException e) { 
    System.out.println("Invalid input, sorry."); //This is given on invalid input. Put whatever type of error message you want here. 
    return; 
} 
switch (cmd) { 
//... 
+0

謝謝Jim Garrison,但是同樣的錯誤仍然存​​在 – 2014-10-20 20:45:04

+0

線程「main」中的異常java.lang.Error:未解決的編譯問題: \t無法爲源級別低於1.7的字符串開啓String類型的值。只有轉換int類型或枚舉變量允許 (Application.java:33) \t在ui.Application.main(Application.java:13)\t在ui.Application.runCLI – 2014-10-20 20:45:20

+0

@NadiaS你改變'開關(命令)'切換(cmd)'? – Pokechu22 2014-10-20 20:53:55