Is it possible to write a user-created switch case option? For example, if I had:
String name = JOptionPane.showInputDialog(null, "Please enter your name."
switch (name) {
case "Adam":
JOptionPane.showMessageDialog(null, "Your name is Adam";
case "Julie":
JOptionPane.showMessageDialog(null, "Your name is Julie";
I want to write a program where, when the program prompts the user for a name, if the name is not one of the cases listed, the program will create a new case for it with the exact same executed code for each case. Is this possible?
No, but you wouldn't want to do it like this either. There are so many names in the world! I hear someone in Australia actually tried to name their kid 'Batman'. Do you really want to update your software for these guys?
You can skip the branching
switch
statement and just embed the name data programmatically.