# Basic Calculator: Do While loop not terminating

Page 1 of 1

## 6 Replies - 1699 Views - Last Post: 04 October 2012 - 01:54 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=294269&amp;s=e5d9aad981a758383301e5cf8d68aa40&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 ashkul88

Reputation: 0
• Posts: 4
• Joined: 04-October 12

# Basic Calculator: Do While loop not terminating

Posted 04 October 2012 - 11:17 AM

Hi there,

First time user, only started learning Java yesterday. Trying to build a basic calculator but the do while loop doesn't terminate like it should. Really appreciate any help. Btw, this is personal interest not a homework assignment lol (I decided to teach myself Java by following Youtube lectures).

```import java.util.Scanner;

public class Class3 {
public static void main(String args[]){
Scanner var = new Scanner(System.in);
Scanner opn = new Scanner(System.in);
Scanner option = new Scanner(System.in);
double v1, v2, ans = 0;
String op;
String opt = "Y";

do{

System.out.println("Enter first number:");
v1 = var.nextDouble();
System.out.println("Enter operation (+,-,*,/):");
op = opn.nextLine();
System.out.println("Enter second number:");
v2 = var.nextDouble();

switch (op){
case "+":
ans = v1 + v2;
System.out.println(v1 + "  " + op + "  " + v2 + "  =  " + ans);
break;
case "-":
ans = v1 - v2;
System.out.println(v1 + "  " + op + "  " + v2 + "  =  " + ans);
break;

case "*":
ans = v1 * v2;
System.out.println(v1 + "  " + op + "  " + v2 + "  =  " + ans);
break;
case "/":
ans = v1 / v2;
System.out.println(v1 + "  " + op + "  " + v2 + "  =  " + ans);
break;
default:
System.out.println("Error, invalid operation!");
break;
}

System.out.println("Perform another calculation? Type Y or N and hit Enter:");

opt = option.nextLine();

System.out.println("Option chosen:  " + opt);

} while (opt != "N");

System.out.println("Thank you for using Graphocalc 2012. Peace out homie!");

}

}

```

Cheers
Ash

Is This A Good Question/Topic? 0

## Replies To: Basic Calculator: Do While loop not terminating

### #2 Kakerergodt

Reputation: 89
• Posts: 201
• Joined: 01-May 12

## Re: Basic Calculator: Do While loop not terminating

Posted 04 October 2012 - 11:25 AM

==, !=, etc. should(usually) not be used with String(or other Objects) since they only compare the value if it is a primitive datatype(int, long, byte, etc.), otherwise it compares the reference. So change it to:
```while(!opt.equals("N"))
```

This post has been edited by Kakerergodt: 04 October 2012 - 11:26 AM

### #3 ashkul88

Reputation: 0
• Posts: 4
• Joined: 04-October 12

## Re: Basic Calculator: Do While loop not terminating

Posted 04 October 2012 - 11:41 AM

Thanks for the quick reply Kakerergodt. Works perfectly.

Can I ask what the "!" mark before opt signifies?

### #4 pbl

• There is nothing you can't do with a JTable

Reputation: 8378
• Posts: 31,956
• Joined: 06-March 08

## Re: Basic Calculator: Do While loop not terminating

Posted 04 October 2012 - 12:06 PM

not

== equals
!= not equal

### #5 ashkul88

Reputation: 0
• Posts: 4
• Joined: 04-October 12

## Re: Basic Calculator: Do While loop not terminating

Posted 04 October 2012 - 01:18 PM

pbl, on 04 October 2012 - 12:06 PM, said:

not

== equals
!= not equal

oh ok thanks. i would've expected it to be:

while(opt.!equals("N"))

### #6 Kakerergodt

Reputation: 89
• Posts: 201
• Joined: 01-May 12

## Re: Basic Calculator: Do While loop not terminating

Posted 04 October 2012 - 01:31 PM

ashkul88, on 04 October 2012 - 01:18 PM, said:

pbl, on 04 October 2012 - 12:06 PM, said:

not

== equals
!= not equal

oh ok thanks. i would've expected it to be:

while(opt.!equals("N"))

equals() is a method that returns either true or false, in this case you are using the object "opt" which is a String and has a method, equals(), which compares itself to an incoming parameter, here "N".
If the two matches it essentially says, true, if not, false.

while(opt.equals("N")) will be read as either while(true) or while(false), that is why the ! goes first, and since you wanted it to continue when the statement was "false" you had to negate the false value: while(!false), since the while loop will only continue when whatever's inside it is true.

### #7 ashkul88

Reputation: 0
• Posts: 4
• Joined: 04-October 12

## Re: Basic Calculator: Do While loop not terminating

Posted 04 October 2012 - 01:54 PM

Awesome! That makes perfect sense now.