A quick alternative to many if statements

I start in java and I make a simple program in which I enter something, and if what I type corresponds to one of the things in the "database", then it will print some text. Is there an easier way to check this out rather than doing this:

int 1; int 2; int 3; 

and etc.

 if([USER INPUT].equals("1")) { System.out.println("TEST"); } 

400 times.

+6
source share
5 answers

Use a switch statement or HashMap.

The switch: statement is read, but compiled in a similar (if not identical) if-else chain.

 switch([USER_INPUT]) { case 1: System.out.println("TEST"); break; case 2: System.out.println("HELLO"); break; // And so on. } 

Hash map : much more readable and simpler. This is preferable.

 // Initialization. Map<Integer,String> map = new HashMap<Integer,String>(); map.put(1,"TEST"); map.put(2,"HELLO"); // Printing. String s = map.get(USER_INPUT); if (s == null) System.out.println("Key doesn't exist."); System.out.println(s); 
+8
source

Use a HashMap with the key as Integer and the value as text.

 System.out.println(myMap.get(USER_INPUT)); 

Where did you do myMap.put(1, "TEST"); etc., this greatly increases your OO code.

the basic bytecode of the switch and, if they are very comparable, and I personally do not see any advantage of switching to the switch (if you do not want to fail, which means that it does not include the break statement).

+1
source

A fun alternative would be to use an enumeration. This will work if you want to define all the values ​​in the class. This will simplify the code used to get the text value. And that gives you a few more extra features besides what the switch statement will give you.

 enum NumberText { HELLO(1), WORLD(2); private static final HashMap<Integer,NumberText> map = new HashMap<Integer,NumberText>(); static{ for (ConnectionGenerator c : ConnectionGenerator.values()) { map.put(c.code, c); } } Integer code; NumberText(Integer pCode) { this.code = pCode; } Static ConnectionGenerator getTextFor(Integer code) { return map.get(code); } } 

Then, to get the text, simply do the following:

 NumberText nt = NumberText.getTextFor(USER_INPUT); System.out.println(nt); 

You can get a fancier and put an extra constructor variable in the enumeration and have a specific line of text.

 enum NumberText { HELLO(1, "Hello to You"), GOODBYE(2, "Goodbye"); private static final HashMap<Integer,NumberText> map = new HashMap<Integer,NumberText>(); static{ for (ConnectionGenerator c : ConnectionGenerator.values()) { map.put(c.code, c); } } Integer code; String text; NumberText(Integer pCode, String pText) { this.code = pCode; this.text = pText; } ConnectionGenerator getNumberTextFor(Integer code) { return map.get(code); } getText() { return this.text; } } 

Then you can get the text as follows:

 NumberText.getNumberTextFor(USER_INPUT).getText(); 
+1
source

Use the switch .

 switch(i){ case 1: System.out.println("Hi"); break; case 2: System.out.println("Ok"); break; // ... } 
0
source

You can use the switch statement.

Here is a quick guide and a more detailed explanation. http://docs.oracle.com/javase/tutorial/java/nutsandbolts/switch.html

0
source

All Articles