0
package practiceapplication;
import static java.lang.Integer.parseInt;

class Practiceapplication{

static int calculate(String arguments[]){
    int sum = 0;

if (arguments[0] == "+")   //How do I use .equals() method at this point?
    for(int x = 0; x < arguments.length; x++){
        arguments = Integer.parseInt(arguments);
        sum += arguments[x]; 
 }
     return sum;


if (arguments[0] == "*") {

    for(int x =0; x < arguments.length; x++){
        arguments =Integer.parseInt(arguments[]);
        sum *= arguments[x];
    }
} return sum;
if (arguments[0] == "-"){
    for(int x = 0; x< arguments.length; x++){
        arguments = Integer.parseInt(arguments);
        sum -= arguments[x];
    }
} return sum;

if(arguments[0] == "/"){
    for(int x =0; x< arguments.length; x++){
        arguments = Integer.parseInt(arguments);
        sum /= arguments[x];


        }
    } return sum;


}
public static void main(String[] arguments){
    if(arguments.length > 0)
        Practiceapplication.calculate(arguments);
    System.out.print("The answer is: " + sum);     //Why was there an err at "sum"?
}
}

I just started learning java, so I don't know much. I apologize if I frustrate you, but hey, no one starts out from knowing everything.

Anyways, I think you get the idea what kind of application I was trying to make. I wanted to sum up everything I know into this thing, so it might look messy. Anyways, could someone tell me what's wrong, and possibly edit the parts where I made mistakes, please?

Thank you!

5
  • 2
    possible duplicate of How do I compare strings in Java? Commented Jul 25, 2013 at 7:35
  • Error when using parseInt() and other errors: if you want to learn, then read the error messages. They tell you what is wrong, and where. If you don't understand the error messages, post them. That's how experimented developers fix errors: by reading them and understanding them. Commented Jul 25, 2013 at 7:36
  • 3
    @JBNizet did you really mean experimented developers? Commented Jul 25, 2013 at 7:37
  • :_____________________( Commented Jul 25, 2013 at 7:44
  • @MarounMaroun i suffer with you... every single time someone does this Commented Jul 25, 2013 at 7:50

3 Answers 3

5
if (arguments[0] == "+")   //How do I use .equals() method at this point?

Use this:

if ("+".equals(arguments[0]))

Learn more about string comparision, from this related post : Java String.equals versus ==

And for errors related to parseInt:

You just need to make sure, you are passing a valid number string(with digits) to the parseInt method. If you don't do it then it will throw a numberformatexception.

Sign up to request clarification or add additional context in comments.

Comments

0

You've got several problems in your code. Most probably you should read into some Java tutorials first!

(1) You can compare Strings using arguments[0].equals("+") source
(2) Code in your calculate() method does not execute after a return statement.
(3) Familiarize yourself with arrays and methods in Java

Still, here is the working code, hoping you can learn something from it:

static int calculate(String arguments[]) {
    int sum = 0;

    if (arguments[0].equals("+")) {
        for (int x = 0; x < arguments.length; x++) {
            int arg = Integer.parseInt(arguments[x]);
            sum += arg;
        }
    } else if (arguments[0].equals("*")) {
        for (int x = 0; x < arguments.length; x++) {
            int arg = Integer.parseInt(arguments[x]);
            sum *= arg;
        }
    } else if (arguments[0].equals("-")) {
        for (int x = 0; x < arguments.length; x++) {
            int arg = Integer.parseInt(arguments[x]);
            sum -= arg;
        }
    } else if (arguments[0].equals("/")) {
        for (int x = 0; x < arguments.length; x++) {
            int arg = Integer.parseInt(arguments[x]);
            sum /= arg;
        }
    }
    return sum;

}

public static void main(String[] arguments) {
    int result = 0;
    if (arguments.length > 0)
        result = Practiceapplication.calculate(arguments);
    System.out.print("The answer is: " + result);
}

Comments

0

//Why was there an err at "sum"? Take return value in some variable

public static void main(String[] arguments){
    if(arguments.length > 0)        
    System.out.print("The answer is: " + Practiceapplication.calculate(arguments););     
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.