#JDM discord bot more efficient way for if statements with multiple variables.

24 messages · Page 1 of 1 (latest)

bright mural
#
public static String modTwo(String modtwo,String mod,String textOption){
String reply;
if(mod.equalsIgnoreCase("bold")&&modtwo.equalsIgnoreCase("italic")){reply = "***"+textOption+"***";
}else if(mod.equalsIgnoreCase("bold")&&modtwo.equalsIgnoreCase("under")){reply = "**__"+textOption+"__**";
}else if(mod.equalsIgnoreCase("bold")&&modtwo.equalsIgnoreCase("strike")){reply = "**~~"+textOption+"~~**";
}else if(mod.equalsIgnoreCase("bold")&&modtwo.equalsIgnoreCase("bold")){reply = "Duplicate mod(s) detected: invalid choice(s)";
}else if(mod.equalsIgnoreCase("strike")&&modtwo.equalsIgnoreCase("italic")){reply = "~~*"+textOption+"*~~";
}else if(mod.equalsIgnoreCase("strike")&&modtwo.equalsIgnoreCase("under")){reply = "~~__"+textOption+"__~~";
}else if(mod.equalsIgnoreCase("strike")&&modtwo.equalsIgnoreCase("bold")){reply = "~~**"+textOption+"**~~";
}else if(mod.equalsIgnoreCase("strike")&&modtwo.equalsIgnoreCase("strike")){reply = "Duplicate mod(s) detected: invalid choice(s)";
}else if(mod.equalsIgnoreCase("under")&&modtwo.equalsIgnoreCase("italic")){reply = "__*"+textOption+"*__";
}else if(mod.equalsIgnoreCase("under")&&modtwo.equalsIgnoreCase("bold")){reply = "__**"+textOption+"**__";
}else if(mod.equalsIgnoreCase("under")&&modtwo.equalsIgnoreCase("strike")){reply = "__~~"+textOption+"~~__";
}else if(mod.equalsIgnoreCase("under")&&modtwo.equalsIgnoreCase("under")){reply = "Duplicate mod(s) detected: invalid choice(s)";
}else if(mod.equalsIgnoreCase("italic")&&modtwo.equalsIgnoreCase("bold")){reply = "***"+textOption+"***";
}else if(mod.equalsIgnoreCase("italic")&&modtwo.equalsIgnoreCase("under")){reply = "*__"+textOption+"__*";
}else if(mod.equalsIgnoreCase("italic")&&modtwo.equalsIgnoreCase("strike")){reply = "*~~"+textOption+"~~*";
}else if(mod.equalsIgnoreCase("italic")&&modtwo.equalsIgnoreCase("italic")){reply = "Duplicate mod(s) detected: invalid choice(s)";
}else{
reply = "Invalid Option(s).";
}
return reply;
}

I had to remove the indentation for discord

wary spadeBOT
#

This post has been reserved for your question.

Hey @bright mural! Please use /close or the Close Post button above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

bright mural
#

Honestly unsure if that title was the best way to name it but essentially I have 3 variable which are mod, modtwo, and modthree(mod standing for modifier ie. Bold, Underline, etc). I want it to be able to realize what those mean in different cases and concatenate things onto the message, such as double asterisks for bold or 1 for italic. As of right now I have written 64 lines for each case of using the 3 variables but I would like to automate this process.

I'm going to put the methodfor just using 2 of the variables as 16 lines is a lot better than 64.

wary spadeBOT
#
102ms
#

<@&765578700724371486>

Requested by 𝗠𝗶𝗹𝗹𝗹𝗹𝗶𝗼𝗻#1956
jaunty veldt
#

Don't attach with &&

bright mural
#

How would I go about doing that, I have tried a couple different things and they all end in errors

jaunty veldt
#

Use switch statement, or preferably expression

#

Use a helper method.

#

Do you need a sample?

bright mural
#

That would be great

jaunty veldt
#

./run

class Test {
private static String applyMD(final String msg, final String modifier) {
    switch (modifier.toLowerCase()) {
        case "bold": return "**" + msg + "**";
        case "italic": return "*" + msg + "*";
        default: return msg;
    }
}

public static String modify(String base,  final String... modifiers) {
    for (String modifier: modifiers) 
        base = applyMD(base, modifier);

    return base;
}

public static void main(String... args) {
    System.out.println(modify("Hello", "italic"));
System.out.println(modify("Hello", "bold", "italic"));
System.out.println(modify("Hello", "italic", "bold"));
System.out.println(modify("Hello"));
System.out.println(modify("Hello", "weird"));
}

}
fringe oreBOT
#

Here is your java(15.0.2) output @jaunty veldt

*Hello*
***Hello***
***Hello***
Hello
Hello
bright mural
#

Holy hell, Thank you so much! I also have another question if it's not too much to ask. Where did you learn Java? I'm taking a class at school but even with it being an AP its a pretty slow paced class.

jaunty veldt
#

Well I helped a friend clean up java code for their project

#

And then I kinda just got used to the syntax

#

1-2 months was a bit of an issue to get used to, and then the graph was smooth

bright mural
#

Okay, thank you again so much, you were a great help!

jaunty veldt
#

So yeah I kinda just saw code and tried to understand it

bright mural
#

sorry for the compact code but discord has a message limit that made it a little hard to paste in

jaunty veldt
bright mural
#

Thank you