#Code issue, don't know the right line of code
1 messages · Page 1 of 1 (latest)
package com.zerox51.test2.commands;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
public class calcCommand implements CommandExecutor
{
@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
int numOfArguments = args.length;
int value1 = Integer.parseInt(args[0]);
char special = Character.parseChar(args[1]);
int value2 = Integer.parseInt(args[2]);
int sum = value1 special value2;
sender.sendMessage ("Answer " + sum);
return true;
}
}
Character.parseChar(args[1]); is not a real command and i dont know what kind of command could replace what i hoped worked.
String#toCharArray -> char[]
String#charAt(int) -> char
Your special character in the center of your sum values won't work though
you need to parse the char i n a switch statement or map
Now that is alot of concepts i havent heard of yet.
so too ways of doing this either switch
switch(special){
'+' -> {
// logic here
}
'-' -> {
// logic here
}
}
and so on
You would have to map different values of special to operations, basically easiest way is
if special == +
sendMessage(val1+val2)
But there are some other ways also
ScriptEngineManager mgr = new ScriptEngineManager();
ScriptEngine engine = mgr.getEngineByName("JavaScript");
String foo = "40+2";
System.out.println(engine.eval(foo));```
give me a minute to try mentally consume this
Cause essentially you wanna do sth different with val1 and val2 dependent on the special variable’s value
sth?
Something
oh
if special == +
sendMessage(val1+val2) so i could do a bunch of if statements for eac operator?
Yes but then you have the more sophisticated strategies like switch, or if you wanna use a map directly
seems a bit overkill for doing something simple with 2 numbers
it will allow you to evaluate almost any math expression
I mean won't an entire JavaScript parsing engine drag a little weight behind it thats not really needed for something this simple
ill look up a tutorial how switching works, thanks for the assistance.
also its a good learning project 🙂
The issue I see is the time and space inefficiency of using the js engine
woops didnt mean to do a reply
I guess it depends on how extensive he is going. if he's just making a simple abacus its a waste.
Ye
ive been doing java for 5 days im just doing random things to learn basics.
Anythign more and its there so he may as well use it.
if you are using a newer version of java consider using the nicer looking version of switch statement
this is clearer
switch(item){
case 'x' -> {
}
defuault -> {
}
}
vs
switch(item){
case 'x':
break;
default:
break;
}
``` if you need fall throughs the second method is just better though
with the JS engine you can do things like /sum (100/2.5)*10.4
I think i am using the latest
eg
We also have libraries that do it but ye
text parser is a nice starting project imo while I think using tools is nice its also very good to know how things work
I can't seem to get the switching to work properly
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
public class calcCommand implements CommandExecutor
{
@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
int numOfArguments = args.length;
int value1 = Integer.parseInt(args[0]);
int value2 = Integer.parseInt(args[1]);
int sum = 0;
String special = value1 + " " + value2;
switch(special){
case value1 + " + " + value2:
sum = value1 + value2;
break;
case value1 + " - " + value2:
sum = value1 - value2;
break;
case value1 + " / " + value2:
sum = value1 / value2;
break;
case value1 + " * " + value2:
sum = value1 * value2;
break;
}
sender.sendMessage("Answer " + sum);
return true;
}
}
I keep getting java: constant string expression required on all cases
Special is just supposed to be the sign
You are making it val1 plus val2
Than you are changing it again in the cases
Your case should look like this
case "/":
case "+" etc
No logic can or should be done in the cases
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
public class calcCommand implements CommandExecutor
{
@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
int value1 = Integer.parseInt(args[0]);
int value2 = Integer.parseInt(args[1]);
int sum = 0;
;
switch(){
case "+":
sum = value1 + value2;
break;
case "-":
sum = value1 - value2;
break;
case " % ":
sum = value1 % value2;
break;
case " * ":
sum = value1 * value2;
break;
}
sender.sendMessage("Answer " + sum);
return true;
}
}```
I just don't see overall how switch statements work, i cant seem them to trigger when doing /calc 1 + 1 it just throws me a error but doing /calc 1 1 gives me 0 so somehow i cant get the switch statement to trigger @chilly cargo
becuase your not switching anything
so you aren't getting an output
how is the code supposed to know what sign it is if your not providing it with an option to dictate such
you mean when i removed the special from the switch?
yes I'd do something like this for a simple calculator
int value1 = Integer.parseInt(args[0]);
String sign = args[1];
int value2 = Integer.parseInt(args[2]);
i trust you could figure out logic with this
now if you wanted to get more complex you'd want a better parser e.g. this wouldn't support something like 1+1+1 would be more complex
than what I provided should work fine as inputs