Results 1 to 6 of 6
  1. #1
    youngdemetre is offline Member
    Join Date
    Apr 2011
    Posts
    5
    Rep Power
    0

    Default Calculator Issues In Eclipse

    Hello, I'm using Java Eclipse to create a basic calculator. When you hit 1,2,3 or the =,-,+ button it puts 1234567890 in the display, does anyone know what could be causing this to happen? Answer if possible are highly appriciated. Thanks in advance

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by youngdemetre View Post
    Hello, I'm using Java Eclipse to create a basic calculator. When you hit 1,2,3 or the =,-,+ button it puts 1234567890 in the display, does anyone know what could be causing this to happen? Answer if possible are highly appriciated. Thanks in advance
    You've likely got a bug in your code, but what that is without seeing the code is anybody's guess.

  3. #3
    youngdemetre is offline Member
    Join Date
    Apr 2011
    Posts
    5
    Rep Power
    0

    Default Code

    The first code is the main and the next is another class


    Java Code:
    public class calculator_main extends conversion_ui {
    
    	
    	
    	public static void main(String[] args) {
    	
    		
    	conversion_ui n = new conversion_ui();
    	n.ui();
    	}
    
    }



    This is the second class





    Java Code:
    import java.awt.FlowLayout;
    import javax.swing.*;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
    
    
    public class conversion_ui implements ActionListener {
    
    	
    	JFrame frame = new JFrame("Conversion");
    	JPanel panel = new JPanel(new FlowLayout());
    	
    	JTextArea text = new JTextArea(1,20);
    	JButton but1 = new JButton("1");
    	JButton but2 = new JButton("2");
    	JButton but3 = new JButton("3");
    	JButton but4 = new JButton("4");
    	JButton but5 = new JButton("5");
    	JButton but6 = new JButton("6");
    	JButton but7 = new JButton("7");
    	JButton but8 = new JButton("8");
    	JButton but9 = new JButton("9");
    	JButton but0 = new JButton("0");
    	
    	JButton butadd = new JButton("+");
    	JButton butsub = new JButton("-");
    	JButton butmulti = new JButton("*");
    	JButton buteq = new JButton("=");
    	
    	JButton butclear = new JButton("C");
    	
    	Double number1,number2,result;
    	
    	int addc=0,subc=0,multic=0;
    	
    	public void ui()
    	{
    		frame.setVisible(true);
    		frame.setSize(250,200);
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		
    		frame.add(panel);
    		
    		panel.add(text);
    		
    		panel.add(but1);
    		panel.add(but2);
    		panel.add(but3);
    		panel.add(but4);
    		panel.add(but5);
    		panel.add(but6);
    		panel.add(but7);
    		panel.add(but8);
    		panel.add(but9);
    		panel.add(but0);
    		panel.add(butadd);
    		panel.add(butsub);
    		panel.add(butmulti);
    		panel.add(buteq);
    		panel.add(butclear);
    		
    		but1.addActionListener(this);
    		but2.addActionListener(this);
    		but3.addActionListener(this);
    		but4.addActionListener(this);
    		but5.addActionListener(this);
    		but6.addActionListener(this);
    		but7.addActionListener(this);
    		but8.addActionListener(this);
    		but9.addActionListener(this);
    		but0.addActionListener(this);
    		butadd.addActionListener(this);
    		butsub.addActionListener(this);
    		butmulti.addActionListener(this);
    		buteq.addActionListener(this);
    		butclear.addActionListener(this);
    	}
    
    	@Override
    	public void actionPerformed(ActionEvent e) {
    		
    		
    		Object source = e.getSource();
    		
    		
    		if(source==but1);
    		{
    			text.append("1");
    		}
    		if(source==but2);
    		{
    			text.append("2");
    		}
    		if(source==but3);
    		{
    			text.append("3");
    		}
    		if(source==but4);
    		{
    			text.append("4");
    		}
    		if(source==but5);
    		{
    			text.append("5");
    		}
    		if(source==but6);
    		{
    			text.append("6");
    		}
    		if(source==but7);
    		{
    			text.append("7");
    		}
    		if(source==but8);
    		{
    			text.append("8");
    		}
    		if(source==but9);
    		{
    			text.append("9");
    		}
    		if(source==but0);
    		{
    			text.append("0");
    		}
    	
    		if(source==butadd)
    		{ 
    			number1=number_reader();
    		if(addc>0)
    		{
    			result=number1+number2;
    			text.setText(Double.toString(result));
    			
    		}
    		if(subc>0)
    		{
    			result=number1-number2;
    			text.setText(Double.toString(result));
    			
    		}
    			
    		if(multic>0)
    		{
    			result=number1*number2;
    			text.setText(Double.toString(result));
    			
    					
    		}
    		if(source==butsub)
    		{ 
    			number1=number_reader();
    			text.setText("");
    			addc=0;
    			subc=1;
    			multic=0;
    			
    		}	if(source==butmulti)
    		{ 
    			number1=number_reader();
    			text.setText("");
    			addc=1;
    			subc=0;
    			multic=1;
    			
    		}	if(source==buteq)
    		{ 
    			number2=number_reader();
    			text.setText("");
    			addc=1;
    			subc=0;
    			multic=0;
    			
    		}
    		}
    	}
    	
    
    
    		public double number_reader()
    		{
    			
    			Double num1;
    			String s;
    			s=text.getText();
    			num1=Double.valueOf(s);
    			
    			return num1;	
    			
    		}
    		}
    Last edited by Fubarable; 04-30-2011 at 03:12 PM. Reason: code tags added

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    You can't have a semicolon ";" after your condition in an if block like you do here:

    Java Code:
    		if(source==but1);
    		{
    			text.append("1");
    		}

    The Java compiler sees this as this:

    Java Code:
          if (source == but1)
             ;  // this "line" will only be called if source == but1
    
          // the lines below will always be called.
          {
             text.append("1");
          }

    The solution is to get rid of that pesky semicolon:


    Java Code:
    		if(source==but1)     // no semi-colon here!
    		{
    			text.append("1");
    		}

    Also, next time please post your code in the initial question as our mind reading abilities are not as strong as you assume them to be. If you look at your initial question again and see the solution above, how would you expect anyone to guess the problem based on that post?

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Of course you could simplify your code quite a bit. For instance, 10 of those if blocks could be replaced by

    Java Code:
          String btnText = e.getActionCommand();
          if ("1234567890".contains(btnText)) {
             text.setText(text.getText() + btnText);
          }

    Also, I'd use a JTextField rather than a JTextArea for my calculator display

  6. #6
    youngdemetre is offline Member
    Join Date
    Apr 2011
    Posts
    5
    Rep Power
    0

    Default

    Thanks, I'll post the code in the question next time, so the other code the could replace the 10 blocks would that just be in the if(source==add) section? Or in the but1.addActionListener?

Similar Threads

  1. EJB 3 Eclipse Jboss issues ...
    By Manic_Velocity in forum Enterprise JavaBeans (EJB)
    Replies: 0
    Last Post: 03-07-2011, 03:38 PM
  2. Eclipse issues
    By MarkusHendersonicus in forum New To Java
    Replies: 2
    Last Post: 01-12-2011, 05:00 PM
  3. eclipse scrapbook issues
    By yitzyk in forum New To Java
    Replies: 1
    Last Post: 12-24-2010, 09:35 AM
  4. Eclipse performance issues
    By stijnvp in forum Eclipse
    Replies: 2
    Last Post: 12-14-2009, 09:53 AM
  5. Issues with YUI in Eclipse
    By Prabs in forum Eclipse
    Replies: 0
    Last Post: 10-17-2008, 03:07 PM

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
  •