Array as the options for a switch statment
I remember from way back at university using a switch with 'binary search' or 'binary switch'. Something like that, My google foo is broken today. Anyway it goes down like this: You define an array of possible options (Strings usually), some magic happens, and those options in the array become the cases in the switch happens. I do remember that they had to be in alphabetical order for this to work. Can you help me remember the magic? Even a name of what I should be looking at would be fantastic.
Asked by: Emily486 | Posted: 21-01-2022
Answer 1
I think what you are looking for is an Enum.
From the link above...
public enum Day {
SUNDAY, MONDAY, TUESDAY, WEDNESDAY,
THURSDAY, FRIDAY, SATURDAY
}
public class EnumTest {
Day day;
public EnumTest(Day day) {
this.day = day;
}
public void tellItLikeItIs() {
switch (day) {
case MONDAY:
System.out.println("Mondays are bad.");
break;
case FRIDAY:
System.out.println("Fridays are better.");
break;
case SATURDAY:
case SUNDAY:
System.out.println("Weekends are best.");
break;
default:
System.out.println("Midweek days are so-so.");
break;
}
}
....
}
Answered by: Sawyer473 | Posted: 22-02-2022
Answer 2
Did you mean gperf? Or possibly you were referring to the theory (hashing) in general?
http://www.gnu.org/software/gperf/
Answered by: Nicole410 | Posted: 22-02-2022Answer 3
Normally, I wouldn't abuse a switch in such a way (even if I could). Try you might, you won't be able to get arrays to work in a switch statement because it only allows constant values in the case lines. Are you sure that you are not thinking of some pattern like below or an enumeration?
final int RED = 0;
final int YELLOW = 1;
final int BLUE = 2;
final int GREEN = 3;
String[] colors = new String[] { "red", "yellow", "blue", "green" };
switch (color) {
case RED:
System.out.println(colors[RED]);
break;
case YELLOW:
System.out.println(colors[YELLOW]);
break;
...the rest
}
Answered by: David376 | Posted: 22-02-2022
Answer 4
Using an Enum without a switch looks like:
public enum Day {
SUNDAY {
public String tellItLikeItIs() {
return "Weekends are best.";
}
},
MONDAY {
public String tellItLikeItIs() {
return "Mondays are bad.";
}
},
TUESDAY,
WEDNESDAY,
THURSDAY,
FRIDAY {
public String tellItLikeItIs() {
return "TGI Friday.";
}
},
SATURDAY {
public String tellItLikeItIs() {
return "Weekends are best.";
}
}
public String tellItLikeItIs() {
return "Midweek days are so-so.";
}
}
public class TodayIs{
public static void main(String... args) {
Day day = Day.valueOf(args[0].toUppercase());
System.out.println(day.tellItLikeItIs());
}
}
Answered by: Miller781 | Posted: 22-02-2022
Answer 5
public enum Day {
SUNDAY ("sundays are this"),
MONDAY ("mondays are that"),
TUESDAY ("blah"),
WEDNESDAY ("blah"),
THURSDAY ("blah"),
FRIDAY ("blah"),
SATURDAY ("more blah");
private final String tell;
public Day(String tell){
this.tell = tell;
}
public String tellItLikeItIs() {
return this.tell;
}
}
public class TodayIs{
public static void main(String... args) {
Day day = Day.valueOf(args[0].toUppercase());
System.out.println(day.tellItLikeItIs());
I'm new to java enums, but I think this should work too.
Answered by: Kimberly979 | Posted: 22-02-2022Answer 6
If your choice is to use enum and your enum collection is of a big size use:
MAGIC:
If you are working in eclipse, it will do a "magic" for you just you need to write the following (taking into consideration the accepted answer's code):
switch (day) {
}
select the switch
and press Ctrl + 1 and the magic works! (all the enum
values will populate in the case
sections of your switch
block)
Similar questions
Iterate two Vectors of same size different values with one ForEach statment in java
Is it possible to use a single
Vector vector1 = new Vector();
Vector vector2 = new Vector();
Map map = new HashMap();
for(String key:vector1&&String value:vector2)
{
map.put(key,value);
}
or something similar.I am trying to poulate a map with these two vectors.I tried the above for each statement but it gave me a syntax error.
Any help.?
java - android code seems to be giving errors while using switch statment
i was trying to use switch statement for the radiobutton but i am etting errors when i run it. can somebody please help me out. i have pasted my android code below. please help if you know the answer. thanks a lot.
package apt.tutorial;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioGro...
java - Getting Syntax error when trying the "Finally" statment
I've put in a "finally" statement, but I get Syntax error, I don't have any idea on how to correct them, I've tried everything, but to no avail. My class get's the string from the two edit text fields, if the checkbox is checked it saves the two strings to a file, to be called up later, then, if there's nothing in the edit text's, it displays a toast. If it's their first time, their data(user and pass it saved) and, if the...
java - using an if statment in jsp to show alert 'this user name has been taken' from a result from action page going through struts
I am trying to get this if statement to display
<s:hidden id="userNameTest" value="nameTest"></s:hidden>
<s:if test="userNameTest.value.equals('false')">
That name is already in use.
</s:if>
if this happens in my action page
public String createUser()
{
user.set_permission("user");
if(user.get_userName...
java - Wrong delete statment in case when object is updated
in my scenario I have 2 tables: album and image (relationship is one to many).
Here is simplified mapping:
<hibernate-mapping>
<class name="Album" table="album">
<id name="id" column="album_id" type="int">
<generator class="sequence">
<param name="sequence">TestObject_seq</param>
</generator>
</id>
<set name...
java - Missing Return Statment Error
This question already has answers here:
java - Small error I don't know how to fix, " not a statment"
jsp - Java Servlet if statment don't need { } brackets?
I am a beginner and reading through Murach's Java Servlet and JSP... Going through the examples. Kind of stuck at this Ch11 simple shopping cart example.
I would post the whole code here but its really way to long.
I have put the full code on my dropbox link : https://dl.dropboxusercontent.com/u/36625850/Ch11-JSTL.rar
...
java - Missing return statment
I wrote this small program as practice in Java(it simulates the 7 electrical logic gates), as I am currently learning it. But when I try to compile it, it gives me several errors stating "MISSING RETURN STATEMENT", but only for the subroutines that have 2 if statements(AND, OR, NAND and NOR). I am wondering if there is something that I don't know about Java if statements. I am also wondering if there is a way ...
Intro to Java: How to use an if statment when a string doesn't exist
I'm trying to get the code to output when someone only inputs two of their names, but I can't figure it out. I've tried using if (nameFML==null) and (nameFML[2].isEmpty()) but I still get a "exception in thread main java.lang.arrayindexoutofboundsexception: 2" error whenever I type in a name like John Doe. Other than that the program does what it's supposed to.
import java.util.Scanner;
...
Still can't find your answer? Check out these amazing Java communities for help...
Java Reddit Community | Java Help Reddit Community | Dev.to Java Community | Java Discord | Java Programmers (Facebook) | Java developers (Facebook)