Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

This question already has an answer here:

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!

share|improve this question

marked as duplicate by Sotirios Delimanolis, ZouZou, Pshemo, peeskillet, Martijn Courteaux Nov 24 '13 at 19:51

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

    
You have to use equals() when you are comparing Strings. –  Ragnar Nov 24 '13 at 19:42
    
Worked Great! Thanks for the help! –  Ixen Nov 24 '13 at 19:48

5 Answers 5

Compare Strings with .equals instead of ==

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

Or even better:

while(cont.equalsIgnoreCase("Yes"))
share|improve this answer
6  
added tip: use .equalsIgnoreCase() –  ljgw Nov 24 '13 at 19:41
    
@ljgw good idea, I'll add it. –  Octoshape Nov 24 '13 at 19:42

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 ==.

share|improve this answer

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.

share|improve this answer

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

share|improve this answer

change to this

 while(cont.equalsIgnoreCase("Yes"))
share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.