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 2 of 2

Thread: Why does my calculator only return 0.0?

  1. #1
    Junior Member
    Join Date
    Jul 2014
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Why does my calculator only return 0.0?

    I am making a calculator in Java. However, when I press the "=" button on my calculator, it always returns 0.0, no matter what. I don't understand why? The code is below:

    import javax.swing.*;//import the packages needed for gui
    import java.awt.*;
    import java.awt.event.*;
    import java.util.Arrays;
    import java.util.List;
    import static java.lang.Math.*;
    public class CalculatorCopy {
        public static void main(String[] args) {
    	JFrame window = new JFrame("Window");//makes a JFrame
    	    window.setSize(300,415); 
    	window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	JPanel panel = new JPanel (new FlowLayout());//makes the panel, textfield and buttons and gives the buttons actioncommands
        final JTextField textField = new JTextField(20);
    	JButton openbracket = new JButton("(");
    		openbracket.setActionCommand("(");
    	JButton closebracket = new JButton(")");
    	closebracket.setActionCommand(")");
    	JButton clearbutton = new JButton("C");
    	clearbutton.setActionCommand("C");
    	JButton arcsin = new JButton("arcsin");
    	arcsin.setActionCommand("arcsin");
    	JButton arccos = new JButton("arccos");
    	arccos.setActionCommand("arccos");
    	JButton arctan = new JButton("arctan");
    	arctan.setActionCommand("arctan");
    	JButton sin = new JButton("sin");
    	sin.setActionCommand("sin");
    	JButton cos = new JButton("cos");
    	cos.setActionCommand("cos");
    	JButton tan = new JButton("tan");
    	tan.setActionCommand("tan");
    	JButton log = new JButton("log");
        log.setActionCommand("log");
    	JButton seven = new JButton("7");
    	seven.setActionCommand("seven");
    	JButton eight = new JButton("8");
    	eight.setActionCommand("eight");
    	JButton nine = new JButton("9");
    	nine.setActionCommand("nine");
    	JButton four = new JButton("4");
    	four.setActionCommand("four");
    	    JButton five = new JButton("5");
    	    five.setActionCommand("five");
    	    JButton six = new JButton("6");
    	    six.setActionCommand("six");
    	    JButton one = new JButton("1");
    	    one.setActionCommand("one");
    	    JButton two = new JButton("2");
    	    two.setActionCommand("two");
    	    JButton three = new JButton("3");
    	    three.setActionCommand("three");
    	    JButton zero = new JButton("0");
    	    zero.setActionCommand("zero");
    	    JButton radixpoint = new JButton(".");
    	    radixpoint.setActionCommand("radixpoint");
    	    JButton plus = new JButton("+");
    	    plus.setActionCommand("plus");
    	    JButton subtract = new JButton("-");
    	    subtract.setActionCommand("subtract");
    	    JButton multiply = new JButton("x");
    	    multiply.setActionCommand("multiply");
    	    JButton divide = new JButton("/");
    	    divide.setActionCommand("divide");
    	    JButton equal = new JButton("=");
    	    equal.setActionCommand("equal");
    	    final String values = " ";
    	    class Listener implements ActionListener {	
    	    	String out = "";
    	     public void actionPerformed(ActionEvent e) {   		  
    		    String output = e.getActionCommand();
    		    if(output == "(") {
                   out = out + "(";
                   textField.setText(out);   
    		    } else if (output == ")") {
    		    out = out + ")";
    		    textField.setText(out);	
    		    }	else if (output == "C") {
    		    	textField.setText(" ");
    		    	out = "";
    		    }  else if (output == "arcsin") {
    		    	out = out + "asin(";
    		    	textField.setText(out);	
    		    }	else if (output == "arccos") {
    		    	out = out + "acos(";
    		    	textField.setText(out);
    		    }	 else if (output == "arctan") {
    		    	out = out + "atan(";
    		    	textField.setText(out);
    		    }   else if (output == "log") {
    		    	out = out + "log(";
    		    	textField.setText(out);	
    		    }   else if (output == "seven") {
    		    	out = out + "7";
    		    	textField.setText(out);
    		    }   else if (output == "eight") {
    		    	out = out + "8";
    		    	textField.setText(out);
    		    }   else if (output == "nine") {
    		    	out = out + "9";
    		    	textField.setText(out);
    		    }   else if (output == "four") {
    		    	out = out + "4";
    		    	textField.setText(out);
    		    }   else if (output == "five") {
    		    	out = out + "5";
    		    	textField.setText(out);
    		    }   else if (output == "six") {
    		    	out = out + "6";
    		    	textField.setText(out);
    		    }   else if (output == "one") {
    		    	out = out + "1";
    		    	textField.setText(out);
    		    }   else if (output == "two") {
    		    	out = out + "2";
    		    	textField.setText(out);
    		    }   else if (output == "three") {
    		    	out = out + "3";
    		    	textField.setText(out);
    		    }   else if (output == "zero") {
    		    	out = out + "0";
    		    	textField.setText(out);
    		    }   else if (output == "radixpoint") {
    		    	out = out + ".";
    		    	textField.setText(out);
    		    }   else if (output == "equal") {
    		    	String[] numbers = out.split("[ +-x/doublesintarclg]", 0); //separate the numbers from the operations
    		    	String[] operations = out.split("[ 1234567890.()]", 0);
    		    	double[] realnumbers = new double[100]; //make an array of doubles
    		    	double answer = 0;
    		    	for ( String number: numbers ) {
    		    	for (double realnumber : realnumbers ) {
    		    	realnumber =  Double.parseDouble(number); //make the string number a double value
    		    	}		
    		    	}	
                    for (String op : operations ) {
                    	if (op == "x") {
                    	  answer = answer + realnumbers[Arrays.asList(operations).indexOf(op)]*realnumbers[Arrays.asList(operations).indexOf(op)+1]; 
                    	}
                    	if (op == "/") {
                    	answer = answer + realnumbers[Arrays.asList(operations).indexOf(op)]/realnumbers[Arrays.asList(operations).indexOf(op)+1];
                    	}
                    	if (op == "+") {
                    	answer = answer + realnumbers[Arrays.asList(operations).indexOf(op)]+realnumbers[Arrays.asList(operations).indexOf(op)+1];	
                    	}
                    	if (op == "-") {
                    	answer = answer + realnumbers[Arrays.asList(operations).indexOf(op)]-realnumbers[Arrays.asList(operations).indexOf(op)+1];
                    	}
                    	String stringofanswer = String.valueOf(answer);
                    	textField.setText(stringofanswer);
     
                    }
    		    }   else if (output == "plus") {
    		    	out = out + "+";
    		    	textField.setText(out);
    		    }   else if (output == "subtract") {
    		    	out = out + "-";
    		    	textField.setText(out);
    		    }   else if (output == "multiply") {
    		    	out = out + "x";
    		    	textField.setText(out);
    		    }   else if (output == "divide") {
    		    	out = out + "/";
    		    	textField.setText(out);
    		    }
    		}
    	}
    	Listener listener = new Listener(); //makes an object of the actionlistener
     panel.add(textField);//adding all the things
    	    window.add(panel);
    	openbracket.addActionListener(listener);    
    	panel.add(openbracket);
    	closebracket.addActionListener(listener);
    	panel.add(closebracket);
    	clearbutton.addActionListener(listener);
    	panel.add(clearbutton);
    	arcsin.addActionListener(listener);
    	panel.add(arcsin);
    	arccos.addActionListener(listener);
    	panel.add(arccos);
    	arctan.addActionListener(listener);
    	panel.add(arctan);
    	sin.addActionListener(listener);
    	panel.add(sin);
    	cos.addActionListener(listener);
    	panel.add(cos);
    	tan.addActionListener(listener);
    	panel.add(tan);
    	log.addActionListener(listener);
    	panel.add(log);
    	nine.addActionListener(listener);
    	panel.add(nine);
    	eight.addActionListener(listener);
    	panel.add(eight);
    	seven.addActionListener(listener);
    	panel.add(seven);
    	six.addActionListener(listener);
    	panel.add(six);
    	five.addActionListener(listener);
    	panel.add(five);
    	four.addActionListener(listener);
    	panel.add(four);
    	three.addActionListener(listener);
    	panel.add(three);
    	two.addActionListener(listener);
    	panel.add(two);
    	one.addActionListener(listener);
    	panel.add(one);
    	zero.addActionListener(listener);
    	panel.add(zero);
    	radixpoint.addActionListener(listener);
    	panel.add(radixpoint);
    	plus.addActionListener(listener);
    	panel.add(plus);
    	subtract.addActionListener(listener);
    	panel.add(subtract);
    	multiply.addActionListener(listener);
    	panel.add(multiply);
    	divide.addActionListener(listener);
    	panel.add(divide);
    	equal.addActionListener(listener);
    	panel.add(equal);
    	window.setVisible(true);
    }
    }

    What's wrong with my code?


  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: Why does my calculator only return 0.0?

    First off, stop comparing Strings using the == operator. Use the equals() method instead. Do a search in this forum or on google for way more information on why that is.

    Secondly, what happened when you stepped through this with a debugger, or at least added some print statements, to figure out when the program's execution differs from what you expect?

    You should try creating an MCVE instead of posting your entire project. Hardcode the String input and create a method that does what the equals button should do. We don't need to see any GUI code if your only problem is converting a String into a math equation.
    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!

Similar Threads

  1. can i return two values from a return method?
    By tonu in forum Object Oriented Programming
    Replies: 4
    Last Post: January 1st, 2014, 11:02 AM
  2. Replies: 1
    Last Post: July 29th, 2013, 06:22 AM
  3. [SOLVED] return
    By dipakshah8944 in forum What's Wrong With My Code?
    Replies: 6
    Last Post: July 16th, 2012, 12:21 AM
  4. return;
    By tarkal in forum Java Theory & Questions
    Replies: 3
    Last Post: November 26th, 2011, 10:53 AM
  5. Return Object does not return the expected output
    By Nour Damer in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 13th, 2011, 07:24 AM

Tags for this Thread