#Code issue, don't know the right line of code

1 messages · Page 1 of 1 (latest)

dense tulip
#

I've been studying java for the past 5 days and i tried to make a calculator today with the help of a tutorial, the tutorial made a calculator with two int variables, but i wanted to make a variable for the operator but i don't know what to change besides replacing the interger with char

#

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.

chilly cargo
#

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

dense tulip
#

Now that is alot of concepts i havent heard of yet.

chilly cargo
#

so too ways of doing this either switch

switch(special){
  '+' -> {
    // logic here
  }
  '-' -> {
    // logic here
  }
}

and so on

cinder terrace
#

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

sacred tree
#
    ScriptEngineManager mgr = new ScriptEngineManager();
    ScriptEngine engine = mgr.getEngineByName("JavaScript");
    String foo = "40+2";
    System.out.println(engine.eval(foo));```
dense tulip
#

give me a minute to try mentally consume this

cinder terrace
#

Cause essentially you wanna do sth different with val1 and val2 dependent on the special variable’s value

dense tulip
#

sth?

cinder terrace
#

Something

dense tulip
#

oh

#

if special == +
sendMessage(val1+val2) so i could do a bunch of if statements for eac operator?

chilly cargo
#

yea I reccomend using a switch here

#

for every operator

sacred tree
#

Just use teh built in JavaScript engin and it does it for you

#

was added in Java 1.6

cinder terrace
#

Yes but then you have the more sophisticated strategies like switch, or if you wanna use a map directly

chilly cargo
sacred tree
#

it will allow you to evaluate almost any math expression

chilly cargo
#

I mean won't an entire JavaScript parsing engine drag a little weight behind it thats not really needed for something this simple

dense tulip
chilly cargo
#

also its a good learning project 🙂

cinder terrace
#

The issue I see is the time and space inefficiency of using the js engine

dense tulip
#

woops didnt mean to do a reply

sacred tree
#

I guess it depends on how extensive he is going. if he's just making a simple abacus its a waste.

cinder terrace
#

Ye

dense tulip
sacred tree
#

Anythign more and its there so he may as well use it.

chilly cargo
# dense tulip woops didnt mean to do a reply

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
sacred tree
#

with the JS engine you can do things like /sum (100/2.5)*10.4

dense tulip
#

I think i am using the latest

sacred tree
#

eg

cinder terrace
#

We also have libraries that do it but ye

chilly cargo
dense tulip
#

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

chilly cargo
#

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

dense tulip
#

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

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

dense tulip
#

you mean when i removed the special from the switch?

chilly cargo
#

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

dense tulip
#

Oh

#

wait why does 2 have arg 0?

chilly cargo
#

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

dense tulip
#

i just want two numbers

#

and a operator

chilly cargo
#

than what I provided should work fine as inputs