How to avoid hardcoding strings in web app - java

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;
}

Related

switch case with integer resources

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;}

Switch Case In BlueJ

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.

Saving variable of unknown subclass type in switch statement

I have a superclass TetrisPiece, with subclasses for each variation of the piece, i.e.
class PieceI extends TetrisPiece{
}
class PieceJ extends TetrisPiece{
}
etc...
In a different class I have a switch statement based on a random number that creates a random piece
switch(rand){
//I
case 1: {
PieceI pieceI = new PieceI();
break;
}
//T
case 2: {
PieceT pieceT = new PieceT();
break;
}
etc...
default:
break;
}
My intention is to extract the piece that is generated from the scope of the switch statement so I can use it later on in the class.
The switch method obviously does not work because of the scope issue, and I cannot create a superclass array outside of the switch statement because I would have no ability to cast the indices due to randomization.
Any help is appreciated.
Create an instance of the superclass TetrisPiece, and then assign PieceT, PieceI, etc to it inside the switch statement.
TetrisPiece piece;
switch(rand){
//I
case 1: {
piece = new PieceI();
break;
}
//T
case 2: {
piece = new PieceT();
break;
}
etc...
default:
break;
}

How to use enum class in switch case

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;
}

Storing variable changes during a switch [Java]

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;
.....

Categories

Resources