Results 1 to 3 of 3
  1. #1
    itaiyz97 is offline Member
    Join Date
    Sep 2012
    Posts
    1
    Rep Power
    0

    Default Simple calculator

    Hey guys! I have been trying to write a simple calculator in java yet I didn't get it to work.
    Here it is:
    Java Code:
     import java.util.*;
    
     public class FirstClass {
    
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args) {
    		System.out.println("Please choose the mathematical action you would like to preform.");
    		Scanner sc= new Scanner(System.in);
    		String x= sc.next();
    		
    		String w=x;
    		if (w=="+") {
    			System.out.println("Please type in the numbers you would like to conjugate");
    			Scanner sd=new Scanner(System.in);
    		    double y= sd.nextDouble();
    		    double z= sd.nextDouble();
    		    double a= y+z;
    		    System.out.println((y+"+"+z+"="+a));
    		} else if (w=="-") {
    			System.out.println("Please type in the number you would like to substract");
    			Scanner se=new Scanner(System.in);
    		    double y= se.nextDouble();
    		    double z= se.nextDouble();
    		    double a= y-z;
    		    System.out.println((y+"-"+z+"="+a));
    		} else {
    			System.err.println("The mathematical function typed was not recognized. Please   type either - or +");
    		}
    	}
     }
    When ever I type + or -, the error message jumps out automatically instead of calculating the doubles.

    Thanks for the help!

    -IZ
    Last edited by JosAH; 09-24-2012 at 05:19 PM. Reason: added [code] ... [/code] tags

  2. #2
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    6

    Default Re: Simple calculator


  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,656
    Blog Entries
    7
    Rep Power
    21

    Default Re: Simple calculator

    You can't compare Strings for equality with the == operator, use the equals( ... ) method instead (read the API documentation for the String class).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Help making a very simple calculator?
    By sfs in forum New To Java
    Replies: 2
    Last Post: 01-26-2012, 03:59 AM
  2. Trying to make simple calculator
    By Buldogg in forum New To Java
    Replies: 1
    Last Post: 09-18-2011, 01:37 PM
  3. Simple Calculator
    By marksey07 in forum New To Java
    Replies: 12
    Last Post: 01-09-2011, 06:30 AM
  4. Re: Simple Calculator cont'd
    By JavaHater in forum New To Java
    Replies: 3
    Last Post: 01-07-2011, 02:00 AM
  5. creating a simple calculator
    By hobo in forum New To Java
    Replies: 4
    Last Post: 11-09-2009, 03:09 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •