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!

  • 2
    possible duplicate of [How do I compare strings in Java?](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – jlordo Jul 25 '13 at 07: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. – JB Nizet Jul 25 '13 at 07:36
  • 3
    @JBNizet did you really mean experimented developers? – Marco Forberg Jul 25 '13 at 07:37
  • :_____________________( – Maroun Jul 25 '13 at 07:44
  • @MarounMaroun i suffer with you... every single time someone does this – Marco Forberg Jul 25 '13 at 07:50

3 Answers3

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.

Community
  • 1
  • 1
Juned Ahsan
  • 67,789
  • 12
  • 98
  • 136
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););     
}
Vinit Prajapati
  • 1,593
  • 1
  • 17
  • 29
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);
}
Michael Lang
  • 3,902
  • 1
  • 23
  • 37