6 Replies - 965 Views - Last Post: 04 October 2012 - 01:54 PM Rate Topic: -----

#1 ashkul88  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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  Icon User is offline

  • D.I.C Head

Reputation: 87
  • View blog
  • 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

Was This Post Helpful? 0
  • +
  • -

#3 ashkul88  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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?
Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

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

Reputation: 8334
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Basic Calculator: Do While loop not terminating

Posted 04 October 2012 - 12:06 PM

not

== equals
!= not equal
Was This Post Helpful? 0
  • +
  • -

#5 ashkul88  Icon User is offline

  • New D.I.C Head

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

Re: Basic Calculator: Do While loop not terminating

Posted 04 October 2012 - 01:18 PM

View Postpbl, 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"))
Was This Post Helpful? 0
  • +
  • -

#6 Kakerergodt  Icon User is offline

  • D.I.C Head

Reputation: 87
  • View blog
  • Posts: 201
  • Joined: 01-May 12

Re: Basic Calculator: Do While loop not terminating

Posted 04 October 2012 - 01:31 PM

View Postashkul88, on 04 October 2012 - 01:18 PM, said:

View Postpbl, 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.
Was This Post Helpful? 0
  • +
  • -

#7 ashkul88  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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.

Thanks so much for the help you guys!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1