How to use String parameters in Javas witch statements
This article mainly explains "how to use String parameters in Javas witch statements". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let the editor take you to learn how to use String parameters in Javas witch statements.
problem
When I try to use the String parameter in the switch statement (note that ctrType is a string)
Switch (ctrType) {case "01": exceptionType = "read FC parameter data"; break;case "03": exceptionType = "read current meter data saved by FC"; break;default:exceptionType = "unknown control code:" + ctrType;}
The following error is prompted:
Cannot switch on a value of type String for source level below 1.7. Only convertible int values or enum variables are permitted
It means that my jre version is too low to support. It is reported that before Java 7, switch could only support byte, short, char, int or their corresponding wrapper classes and Enum types. In Java 7, String support has finally been added.
Solve
General project
Install JDK 1.7 libraries, and change the configuration in the project to introduce the JDK version dependent library.
Maven project
Change the pom.xml file to set the target version of the maven-compiler-plugin plug-in to 1.7, for example
... org.apache.maven.pluginsmaven-compiler-plugin3.21.71.7...
At this point, I believe you have a deeper understanding of "how to use String parameters in Javas witch statements". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!