Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: A basic calculator

  1. #1
    Junior Member
    Join Date
    Dec 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default A basic calculator

    Hey, I'm new to java, and I'm trying to make a calculator, with the choice of + or -. My code is:

    import java.util.Scanner;
    public class Funk {
    public static void main (String[] args) {
     
    	double first;
    	double second;
    	String plussminus;
    	double sum = 0;
    	Scanner input = new Scanner (System.in);
    	Scanner pm = new Scanner (System.in);
    	System.out.print("First number: ");
    	first = input.nextDouble();
     
    System.out.print("+ or -? ");
    plussminus = pm.nextLine();
     
     
     
    	System.out.print("Second number: ");
    	second = input.nextDouble();
     
    	if (plussminus == "plus") {
    	sum = first + second; }
    	else if (plussminus == "minus") {
    	sum = first - second; }
    	else {
    		System.out.println("You did something wrong.");}
     
    	System.out.println(sum);
     
     
    }
    }

    But it always says "You did something wrong.", and i get the answer "0.0". I've tried for quite some time now, but can't get it to work.


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: A basic calculator

    Don't use == to test String equality. Use the equals() method instead. The first tests whether they are the same instance, the second tests whether they are semantically equivalent.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Dec 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: A basic calculator

    Quote Originally Posted by KevinWorkman View Post
    Don't use == to test String equality. Use the equals() method instead. The first tests whether they are the same instance, the second tests whether they are semantically equivalent.
    Thank you! This is the first time i have ever heard of that method at all, so I just learned something new! And thank you for a fast reply

Similar Threads

  1. having bad time with basic calculator code
    By hashey100 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 12th, 2011, 09:29 PM
  2. Calculator Using AWT
    By Allicat in forum What's Wrong With My Code?
    Replies: 7
    Last Post: May 17th, 2011, 06:49 AM
  3. Calculator
    By Andrew Wilson in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 2nd, 2011, 08:08 AM
  4. Calculator
    By javapenguin in forum What's Wrong With My Code?
    Replies: 5
    Last Post: December 22nd, 2010, 09:00 AM
  5. Basic Calculator
    By Yes. in forum What's Wrong With My Code?
    Replies: 13
    Last Post: June 4th, 2010, 04:24 PM