-2
import java.util.Scanner;

   class Lev {                                          //main class


public static void main(String args[]){
    Scanner read = new Scanner(System.in);



    Calculator obj1 = new Calculator();
    Per obj2 = new Per();
    String des;
    System.out.print("Enter your choice= ");
    des = read.nextLine();

    if(des=="calc" || des=="calculator"){

        obj1.calcultr();   

    }

    if(des=="per" || des=="percentage")
    {

        obj2.percentage();  
    }

    else
    {
        System.out.println("Argument not understood");

    }


    read.close();


  }

 }              //main class ends




import java.util.Scanner;                       //2nd class

public class Calculator {


Scanner inp = new Scanner(System.in);

public void calcultr(){

    double num1,num2,strge = 0;
    char q;
    System.out.println("Enter 2 numbers to continue");
    System.out.print("Num1= ");
    num1 = inp.nextDouble();
    System.out.print("Num2= ");
    num2 = inp.nextDouble();
    System.out.print("Enter what do you want to do= ");
    q = inp.next().charAt(0);


        if(q=='+'){

            strge = num1+num2;
            System.out.print("Result= " + strge);
        };

        if(q=='-'){

            strge = num1 - num2;
            System.out.print("Result= " + strge);
        }

        if(q=='*'){

            strge = num1 * num2;
            System.out.print("Result= " + strge);
        }

        if(q=='/')
        {

            strge = num1/num2;
            System.out.print("Result= " + strge);
        }





}


}



import java.util.Scanner;               //3rd class



public class Per {

Scanner inp = new Scanner(System.in);

public void percentage(){

    double numa,from,per;

    System.out.println("Enter total number attained= ");
    System.out.print("Number Attained= ");
    numa = inp.nextDouble();
    System.out.print("Enter total marks from which marks attained= ");
    from = inp.nextDouble();
    per = (numa/from)*100;
    System.out.print("Percentage Achieved= " + per);

}

}
The Hungry Dictator
  • 3,444
  • 5
  • 37
  • 53
Lev
  • 5
  • 4
  • as Sotirios is alluding to ... with string == is comparing if the objects are the same ... not the values. use string.equals(2ndString) instead. – Micho Rizo Apr 17 '14 at 03:14

2 Answers2

1

To compare string, Use equals instead of ==.

Just Example:

if(des.equals("calc") || des.equals("calculator"))

instead of

if(des=="calc" || des=="calculator")

In short,

Use of == : You want to know if two references are to the same object.

Use of equals :Compares values for equality.

bNd
  • 7,512
  • 7
  • 39
  • 72
  • can you show me an example? thanks – Lev Apr 17 '14 at 03:13
  • This answer has an example in it... – takendarkk Apr 17 '14 at 03:14
  • guys it worked thanks a lot but still in the end the else result also shows up when the program terminates – Lev Apr 17 '14 at 03:18
  • I updated answer. check why `equals` not `==`, you are welcome. accept answer if you find help full. – bNd Apr 17 '14 at 03:19
  • and should i use equals in char too? – Lev Apr 17 '14 at 03:20
  • No, char is a primitive data type, so it can be compared with `==`. – bNd Apr 17 '14 at 03:22
  • one more thing when i am applying the first condition it works perfectly but when it ends it shows the else condition too but this doesnt happen when the 2nd if condition is staisfied – Lev Apr 17 '14 at 03:24
  • Change your code with if..else if..else like.., add else before if in second condition.i.e. `else if(des=="calc" || des=="calculator")` instead of just `if(des=="calc" || des=="calculator")` – bNd Apr 17 '14 at 03:27
1

"guys it worked thanks a lot but still in the end the else result also shows up when the program terminates "

You are using two different if statements and the second if statement has an else block to it. What i believe you are trying to do is this

if(des.equals("calc") || des.equals("calculator")){

    obj1.calcultr();   

}

else if(des.equals("per") || des.equals("percentage"))
{

    obj2.percentage();  
}

else
{
    System.out.println("Argument not understood");

}

This should solve your problem with the else still printing out at the end of your program. With your logic you were saying if you choose calc it cals calcultr(), but after that it was saying if you choosed per, which if you already choose calc you wernt, so it printed out the else statement. (If that makes sense, if not i can try and explain it better).

Tango199
  • 52
  • 1
  • 6