I would like to use the switch command with choice defined in a resource file but I have the error:
error: constant expression required
Do you have any suggestion?
ressource file integers.xml
<integer name="readID">0x21</integer>
<integer name="readRevision">0x22</integer>
java file:
switch (cmd) {
case getResources().getInteger(R.integer.readID):
break;
case getResources().getInteger(R.integer.readRevision):
Log.d(TAG, "case revision");
break;
In Java the case part of a switch needs a constant value.
Java expects with getResources().getInteger(R.integer.readID) since it is a method call that the value may change at runtime.
See Java switch statement: Constant expression required, but it IS constant
for more information.
You may use an if, else if, else construct.
Just define your integers as static constants in a separate file (Constants.java perhaps).
Constants
public class Constants{
public static final int READ_ID = 0x11;
public static final int READ_REVISION = 0x22;
}
Switch
switch (cmd) {
case Constants.READ_ID:
break;
case Constants.READ_REVISION:
break;
}
Try
private int getInt(#IntegerRes int res){
return context.getResources().getInteger(res);
}
For example:
switch (cmd) {
case getInt(R.integer.readID):
break;
case getInt(R.integer.readRevision):
Log.d(TAG, "case revision");
break;}
Related
I have a jsp+servlet web application, which runs on tomcat server. All the strings which i use are hardcoded now. If possible I want to move all the hardcoded strings to one resource file and point to particular string in jsp code. How can i do it. Is there any other way other than resource file.For example: In below switch statement, i want to remove all hardcoded strings in case statement and move to one resource file or so and point to that string in my code.
switch (request.getParameter("mode")) {
case "check1": {
break;
}
case "check2": {
break;
}
case "active_inactive": {
break;
}
default:
break;
}
Use class named Constants for this pupose.
public class Constants{
public static String CHECK_1 = "Check1";
public static String CHECK_2 = "Check2";
}
And use this in anywhere you want.
switch (request.getParameter("mode")) {
case Constants.CHECK_1: {
break;
}
case Constants.CHECK_2: {
break;
}
default:
break;
}
In documentation it is said you could equally use if-else multiple times or switch-case:
int condition;
setCondition(int condition) {
this.condition = condition;
}
Either switch-case
switch (condition) {
case 1: print("one"); break;
case 2: print("two"); break;
or
if (condition == 1) { print("one"); }
else if (condition == 2) { print("two"); }
Next, conditionis declared volatile and method setCondition() is called from multiple threads.
If-else is not atomic and volatile variable write is a synchronizing action. So both "one" and "two" string could be printed in the last code.
It could be avoided if some method local variable with initial value was used:
int localCondition = condition;
if (local condition == ..) ..
Does switch-case operator hold some initial copy of variable? How are cross threads operations implemented with it?
From the Java specification on switch statements:
When the switch statement is executed, first the Expression is evaluated. [...]
This suggests that the expression is evaluated once and that the result is temporarily kept somewhere else, and so no race-conditions are possible.
I can't find a definite answer anywhere though.
A quick test shows this is indeed the case:
public class Main {
private static int i = 0;
public static void main(String[] args) {
switch(sideEffect()) {
case 0:
System.out.println("0");
break;
case 1:
System.out.println("1");
break;
default:
System.out.println("something else");
}
System.out.println(i); //this prints 1
}
private static int sideEffect() {
return i++;
}
}
And indeed, sideEffect() is only called once.
The expression is evaluated once when entering the switch.
The switch may use the result internally as many times as it needs to determine what code to jump to. It's akin to:
int switchValue = <some expression>;
if (switchValue == <some case>)
<do something>
else if (switchValue == <some other case>
<do something else>
// etc
Actually, a switch compiles to a variety of byte code styles depending on the number of cases and the type of the value.
The switch only needs to evaluate the expression once.
public class Hello {
public static void main(int a) {
switch (a) {
case 1:
System.out.println("Hi");
}
switch (a) {
case 2:
System.out.println("Hello");
}
}
}
Hi,
I want to know if it is possible for me to use Switch Case for the same variable twice, like I've done in the snippet attached.
Thanks.
The code you have provided works. So long as the variable a is in scope, you can use it for as many switch statements as you like.
If you want to check for multiple values of a in the same switch, then you should use different cases. E.g.:
switch (a) {
case 1:
System.out.println("a was 1");
break; // if we did not break, then execution would "fall-through" to the next case
case 2:
System.out.println("a was 2");
break;
default:
System.out.println("a was not 1 or 2");
}
Find out more about the switch statement in the Java Documentation.
Am getting error - case expressions must be constant expressions , when am trying to use enum class in switch case statements :
My enum class is,
public enum TestEnumClass {
TEST1("TEST1"),
TEST2("TEST2"),
TEST3("TEST3");
private String enumConstant;
private TestEnumClass(String algoConstant) {
this.enumConstant = algoConstant;
}
public String getEnumConstant() {
return enumConstant;
}
}
And am trying to use enum TestEnumClass as below in another class file,
public class TestIndexOf {
public static void main(String[] args) {
String str = args[0];
switch(str){
case TestEnumClass.Test1.getEnumConstant() : System.out.println("test1"); break;
case TestEnumClass.Test2.getEnumConstant() : System.out.println("test2"); break;
}
}
}
Its giving me compile time error :
case expressions must be constant expressions
Please suggest me, where am I going wrong.
You can't use the result of methods as cases in a switch statement. Switches are optimised for constant cases. Enums are very suitable for this, but you would have to have:
TestEnumClass value = TestEnumClass.valueOf(str);
switch (value) {
case TEST1: ...
case TEST2: ...
}
your switch expression is on a String, you need to change it to a variable of type TestEnumClass
You are doing switch over String but the case TestEnumClass.TEST1.getEnumConstant() is not compile time constant according to JLS. A case statement requires compile time constant value. In order to correct the error you can do like this
String str = args[0];
TestEnumClass e = TestEnumClass.valueOf(str);
switch(e){
case TEST1: System.out.println("test1"); break;
case TEST2 : System.out.println("test2"); break;
}
I am currently in the process of making a calculator for Android (I am not using any tutorials) and am running into an issue.
I have this:
public void buttonOnClick(View v){
int operation;
switch (v.getId()){
case R.id.one:
numberBox.append("1");
break;
case R.id.two:
numberBox.append("2");
break;
case R.id.plus:
operation=1;
break;
case R.id.eq:
if (operation == 1){
// Print value
}
break;
default:
break;
}
}
Note that this is not the exact code, it is just a mockup.
The problem is, with the scope of the case, when the operation is set to 1, it is not set publicly and when I go to read it in the equals case, it is set back to 0.
How can I fix this problem?
You must be new in Java or any other programming language:
local variables are only used in the method they are declared in.
Whenever you go out of the method the variable is destroyed as well as its value.
So each time you enter a new variable is initialized.
To make this work just create a global class variable and use it or
Pass an object to the method that holds some int variable : the current operation, then read/write the variable using that object.
In your case its better to use a global class variable:
public class MyClass {
int operation;
public int getOperation() {
return operation;
}
public void setOperation(int value) {
this.operation = value;
}
.....
THEN:
....
public void buttonOnClick(View v){
...
case R.id.plus:
this.operation = 1;
break;
case R.id.eq:
if (this.operation == 1){
// Print value
}
break;
.....