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: I have tried making a simple random calculator and its not working?

  1. #1
    Junior Member
    Join Date
    Oct 2012
    Location
    Switzerland
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default I have tried making a simple random calculator and its not working?

    I have tried making this calculator to get used to working with classes, methods, return statements, etc..., but the method that should return my result won't return anything. I have tried using the debug mode in Eclipse but that didn't help me either.



    Main method:

    public class CalculatorMain { //class CalculatorMain
     
        public static void main(String[] args) { //main method
     
    	Calculator1 a = new Calculator1(); //new class a
     
    	JOptionPane.showMessageDialog(null, "This is a primitive Calculator"); //startup message
     
    	a.input(); //input method
     
    	double y = a.input(); //create y which equals the return value of input method
     
    	JOptionPane.showMessageDialog(null, y); //shows result
     
        }
     
    }

    Calculator method:

    package calculator;
     
    import javax.swing.JOptionPane;
     
    public class Calculator1 {
     
        double b;
        double a;
     
        double input()
        {
     
    	a = Double.parseDouble(JOptionPane.showInputDialog("Enter your first number: ")); //shows input and sets a
    	b = Double.parseDouble(JOptionPane.showInputDialog("Enter your first number: ")); //shows input and sets b
    	String c = JOptionPane.showInputDialog("Type a(dd), s(ubtract), m(ultiply), d(evide)"); //asks for a, b, c or d
     
    	if (c == "a"){
     
    	    return a + b;
     
    	}
    	else if (c == "s"){
     
    	    return a - b;
     
    	}
    	else if (c == "m"){
     
    	    return a * b;
     
    	}
    	else if (c == "d"){
     
    	    return a / b;
     
    	}
     
    	return 0;
     
        }
     
     
    }

    Thanks for any help, I am new to coding and teaching java to myself, so the only place I can go for help is the internet.

    -StackOfCookies


  2. #2
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: I have tried making a simple random calculator and its not working?

    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  3. #3
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default Re: I have tried making a simple random calculator and its not working?

    From the Java String class descriptions of the 'equals()' method at Java String Class:

    equals

    public boolean equals(Object anObject)

    Compares this string to the specified object. The result is true if and only if the argument is not null and is a String object that represents the same sequence of characters as this object.
    Bottom line: Don't use something like (c =="a") to compare one String to another. Use c.equals("a")


    Cheers!

    Z

Similar Threads

  1. My random generator isn't working. Please help!
    By tripline in forum What's Wrong With My Code?
    Replies: 9
    Last Post: November 5th, 2011, 10:03 AM
  2. JButtons making a Phone Calculator
    By falkowa42 in forum AWT / Java Swing
    Replies: 3
    Last Post: October 10th, 2011, 07:03 PM
  3. random not working in .class method
    By Spidey1980 in forum Java SE APIs
    Replies: 13
    Last Post: August 19th, 2011, 07:35 AM
  4. Simple Calculator
    By JKDSurfer in forum Loops & Control Statements
    Replies: 3
    Last Post: June 28th, 2011, 01:37 PM
  5. switch statement not working for JFrame Calculator
    By kaddyshack in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 23rd, 2011, 10:35 AM

Tags for this Thread