Java 如何在扫描程序中使用枚举类型
非常感谢你在广告中的帮助 使用枚举类型的扫描仪时出错。但是,在这个任务中我不允许使用缓冲区(InputStreamReader)。最好的解决办法是什么 我收到以下错误:Java 如何在扫描程序中使用枚举类型,java,enums,java.util.scanner,bufferedreader,Java,Enums,Java.util.scanner,Bufferedreader,非常感谢你在广告中的帮助 使用枚举类型的扫描仪时出错。但是,在这个任务中我不允许使用缓冲区(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
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);
}
静态字符串命令;
公共枚举命令{
连接、断开连接、发送、日志级别、帮助、退出、退出
}
专用静态无效运行(扫描程序sc){
//String命令;//准备输入
boolean done=false;//准备好进入菜单循环
当(!完成){//继续,直到完成
系统输出
.println(“Milestone1:与TCP服务器的连接和交互”);
系统输出
.println(“--------------请选择命令的on以便--------------------”;
System.out.println(“连接”);
系统输出打印项次(“断开”);
System.out.println(“发送”);
System.out.println(“日志级别”);
System.out.println(“帮助”);
System.out.println(“退出”);
System.out.println(“退出”);
command=sc.nextLine();//接受用户输入
命令cmd=null;
试一试{
cmd=Command.valueOf(Command.toUpperCase());
}
捕获(IllegalArgumentException e){
System.out.println(“无效输入”);
返回;
}
开关(cmd){
案例退出://退出菜单
done=true;//中断循环的条件
打破
案例连接:
系统输出打印(“IP地址:”);
试一试{
字符串userInput=sc.toString();//用户输入
System.out.println(“端口:”);
int userInput1=sc.nextInt();//用户输入
如果(用户输入1>=0){
System.out.println(“EcoClient>”+“”+命令+“”
+用户输入+“”+用户输入1);
}否则{
系统输出
.println(“输入的端口值为负数或IP地址长度<7 | |>15,不是n.n.n.n格式”);
}
}
catch(异常e){//在不符合逻辑的情况下抛出异常
//输入
System.out.println(“\n空白输入,请重试”);
sc.nextLine();//删除剩余的“\n”
}
打破
外壳断开:
System.out.println(“EcoClient>”+“”+命令);
打破
案例发送:
系统输出
.println(“请输入“+”Hello World“+”短语”);
试一试{
字符串userInput=sc.toString();//用户输入
System.out.println(“EcoClient>”+“”+命令+“”
+用户输入);
}
catch(异常e){//在不符合逻辑的情况下抛出异常
//输入
System.out.println(“\n空白输入,请重试”);
sc.nextLine();//删除剩余的“\n”
}
打破
案件记录级别:
试一试{
System.out.println(“EcoClient>”+“”+command+”<“
+“当前日志状态”+“>”;
}
catch(异常e){//在不符合逻辑的情况下抛出异常
//输入
System.out.println(“\n空白输入,请重试”);
sc.nextLine();//删除剩余的“\n”
}
打破
案例帮助:
试一试{
系统输出
.println(“以下命令集提供以下功能:”
+“连接:建立与eco服务器的连接”
+“断开连接:断开与服务器的连接并接收确认消息”
+“发送:将消息发送到服务器”
+“日志级别:打印当前日志状态”
+“退出:退出并通知用户程序已关闭”
+“退出:取消输入”);
}
catch(异常e){//在不符合逻辑的情况下抛出异常
//输入
System.out.println(“\n空白输入,请重试”);
sc.nextLine();//删除剩余的“\n”
}
打破
案例退出:
试一试{
System.out.println(“EcoClient>”+命令);
}
catch(异常e){//在不符合逻辑的情况下抛出异常
//输入
System.out.println(“\n空白输入,请重试”);
sc.nextLine();//删除剩余的“\n”
}
打破
违约:
System.out.println(“不识别”
+“输入,pl.重试”);
}
}
}
公共静态void main(字符串[]args){
Scanner sc=new Scanner(System.in);//将接受用户输入
run(sc);
}
}sc.nextLine()
返回一个字符串。您需要使用静态方法Command.valueOf(String)
将其转换为Command
的实例(在该实例上使用switch
),该方法解析字符串并返回匹配的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) {
//...
不要忘记在
main
的声明中使用static
一词,并且在调用run
之前实例化类,或者将run
声明为static。谢谢。但是添加static并不能解决stacktrace提到的问题runCLI
,而不是上面提到的run
?当然,但它解决了一个不同的问题