0
package pack;

import java.util.Scanner;

public class Calculator {


    public static void main(String args[]){
        Scanner scan = new Scanner(System.in);
        String cont = "Yes";

        while(cont == "Yes" || cont == "yes" ){
            System.out.print("Enter a Number: ");
            int x = scan.nextInt();
            System.out.print("Enter another Number: ");
            int y = scan.nextInt();

            int diff = x - y;
            int sum = x + y;
            int prod = x * y;
            int quot = x / y;

            System.out.println("The Sum is: " + sum);
            System.out.println("The Diffrence is: " + diff);
            System.out.println("The Product is: " + prod);
            System.out.println("The quotient is: " + quot);

            System.out.print("Enter Yes to Continue: ");
            cont = scan.next();
            System.out.println(cont);

        }

    }



}

This entire code works, but the while loop doesn't repeat. The cont = scan.next(); is catching the string. The output looks like this:

[

Enter a Number: 5

Enter another Number: 6

The Sum is: 11

The Diffrence is: -1

The Product is: 30

The quotient is: 0

Enter Yes to Continue: Yes

Yes

]

Then the program terminates without any problems. All I need it to get the while loop to repeat. Thanks for the help!

Shevliaskovic
  • 1,562
  • 4
  • 26
  • 43
Logan Ruf
  • 1
  • 1

5 Answers5

8

Compare Strings with .equals instead of ==

while(cont.equals("Yes") || cont.equals("yes") )

Or even better:

while(cont.equalsIgnoreCase("Yes"))
Octoshape
  • 1,131
  • 8
  • 26
1

You need to need to compare the Strings this way:

while("Yes".equalsIgnoreCase(cont))...

That's because when using input, you won't have String literals, but String objects. Those need to be compared via equals() and not ==.

noone
  • 19,520
  • 5
  • 61
  • 76
1

Change

while(cont == "Yes" || cont == "yes" ){

As

while(cont.equalsIgnoreCase("Yes"))

Reason

You need to know the reason behind == and equals()

equals() method is present in the java.lang.Object class and it is expected to check for the equivalence of the state of objects!. That means, the contents of the objects. Whereas the == operator is expected to check the actual object instances are same or not.

Example with if statement

Consider two different reference variables str1 and str2

str1 = new String("abc");
str2 = new String("abc");

if you use the equals()

System.out.println((str1.equals(str2))?"TRUE":"FALSE");

You will get the output as TRUE

if you use ==

 System.out.println((str1==str2)?"TRUE":"FALSE");

Now you will get the FALSE as output because both str1 and str2 are pointing to two different objects even though both of them share the same string content. It is because of new String() everytime a new object is created.

Rakesh KR
  • 6,357
  • 5
  • 40
  • 55
0

instead of (cont == "Yes" || cont == "yes" ) you should use (cont.equals("Yes") || cont.equals("Yes"))

Murat Güvenç
  • 111
  • 1
  • 5
0

change to this

 while(cont.equalsIgnoreCase("Yes"))
subash
  • 3,116
  • 3
  • 18
  • 22