6 Replies - 10331 Views - Last Post: 29 March 2011 - 03:30 AM Rate Topic: -----

#1 bartsimpsong  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 28-March 11

adding two numbers

Posted 28 March 2011 - 08:58 PM

import java.awt.FlowLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JOptionPane;

public class ButtonFrame extends JFrame{
	private JButton plainJButton;

	public ButtonFrame(){
		super("Testing buttons");
		setLayout( new FlowLayout() );

		String fNumber =
			JOptionPane.showInputDialog("Enter first integer");
			int n1 = Integer.parseInt( fNumber );

		String sNumber =
			JOptionPane.showInputDialog("Enter second integer");
			int n2 = Integer.parseInt( sNumber );

		int sum = n1 + n2;

		plainJButton = new JButton("Addition Button");
		add( plainJButton );

		ButtonHandler handler = new ButtonHandler();
		plainJButton.addActionListener( handler );
	}

	private class ButtonHandler implements ActionListener{
		public void actionPerformed( ActionEvent event ){

			JOptionPane.showMessageDialog(null, "The sum is"+ sum,
				"Sum of Two Integers", JOptionPane.PLAIN_MESSAGE);
		}
	}
}



These are the error messages:
cannot find symbol
symbol : variable n1
location: class ButtonFrame.ButtonHandler
int sum = n1 + n2;
And this repeats with n2, and + sign. So I am not sure how to fix it. The task is just to add two numbers using the ActionListener. Thank you

Is This A Good Question/Topic? 0
  • +

Replies To: adding two numbers

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8328
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: adding two numbers

Posted 28 March 2011 - 09:05 PM

public class ButtonFrame extends JFrame{
	private JButton plainJButton;
	// you will have to declare sum here so that your actionPerformed() method can "sees" it
    private int sum;
	
	public ButtonFrame(){
		super("Testing buttons");
		setLayout( new FlowLayout() );

		String fNumber =
			JOptionPane.showInputDialog("Enter first integer");
			int n1 = Integer.parseInt( fNumber );

		String sNumber =
			JOptionPane.showInputDialog("Enter second integer");
			int n2 = Integer.parseInt( sNumber );

		sum = n1 + n2;


Was This Post Helpful? 1
  • +
  • -

#3 bartsimpsong  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 28-March 11

Re: adding two numbers

Posted 28 March 2011 - 09:15 PM

Great! thanks, that was very simple.
Was This Post Helpful? 0
  • +
  • -

#4 bartsimpsong  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 28-March 11

Re: adding two numbers

Posted 28 March 2011 - 09:37 PM

I have another question very much with the same program. Now what I am trying to do is to open one window with two textboxes and one button. One text box to input a number and the other is to output a number. So my question is this; Since I have two textboxes I am not sure how to convert the first box to integer to use it for calculations, and output in the second box the result. My current code just pops up the window with two text boxes and a button but since I don't know how to convert the first box to int there is no calculations. Please any help is appreciated. Thank you

import java.awt.FlowLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

public class ButtonFrame extends JFrame{
	private JButton plainJButton;
	private JTextField textField1;
	private JTextField textField2;

	int sum;

	public ButtonFrame(){
		super("Testing buttons");
		setLayout( new FlowLayout() );

		textField1 = new JTextField( 10 );
		add( textField1 );

		textField2 = new JTextField( 10 );
		textField2.setEditable(false);
		add( textField2 );

		plainJButton = new JButton("Addition Button");
		add( plainJButton );

		ButtonHandler handler = new ButtonHandler();
		plainJButton.addActionListener( handler );
	}

	private class ButtonHandler implements ActionListener{
		public void actionPerformed( ActionEvent event ){

			JOptionPane.showMessageDialog(null, "The sum is"+ sum,
				"Sum of Two Integers", JOptionPane.PLAIN_MESSAGE);
		}
	}
}


This post has been edited by bartsimpsong: 28 March 2011 - 09:40 PM

Was This Post Helpful? 0
  • +
  • -

#5 bartsimpsong  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 28-March 11

Re: adding two numbers

Posted 28 March 2011 - 10:04 PM

Ok I think I have found out one more thing even though it runs still it does not work and the error messages are:

Exception in thread "main" java.lang.NumberFormatException: empty String
at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:99
4)
at java.lang.Double.valueOf(Double.java:475)
at ButtonFrame.<init>(ButtonFrame.java:28)
at ButtonTest.main(ButtonTest.java:5)
Press any key to continue . . .


I believe this line would convert from text to double and should help me to do the conversion:
result = Double.valueOf(textField1.getText()) * kg;


import java.awt.FlowLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

public class ButtonFrame extends JFrame{
	private JButton plainJButton;
	private JTextField textField1;
	private JTextField textField2;

	double result;
	double kg=0.45359237;

	public ButtonFrame(){
		super("Testing buttons");
		setLayout( new FlowLayout() );

		textField1 = new JTextField( 10 );
		add( textField1 );

		textField2 = new JTextField( 10 );
		textField2.setEditable(false);
		add( textField2 );

	result = Double.valueOf(textField1.getText()) * kg;

		plainJButton = new JButton("Addition Button");
		add( plainJButton );

		ButtonHandler handler = new ButtonHandler();
		plainJButton.addActionListener( handler );
	}

	private class ButtonHandler implements ActionListener{
		public void actionPerformed( ActionEvent event ){

			JOptionPane.showMessageDialog(null, "The conversion is"+ result,
				"of a value", JOptionPane.PLAIN_MESSAGE);
		}
	}
}



Thank you.
Was This Post Helpful? 0
  • +
  • -

#6 bartsimpsong  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 28-March 11

Re: adding two numbers

Posted 28 March 2011 - 11:16 PM

Ok, I have made some changes and here is the error messages.

Exception in thread "main" java.lang.NumberFormatException: empty String
at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:99
4)
at java.lang.Double.parseDouble(Double.java:510)
at ButtonFrame.<init>(ButtonFrame.java:22)
at ButtonTest.main(ButtonTest.java:5)
Press any key to continue . . .

import java.awt.FlowLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

public class ButtonFrame extends JFrame{
	private JButton plainJButton;
	private JTextField textField1;
	private JTextField textField2;

	double result=0.0;
	double kg=0.45359237;

	public ButtonFrame(){
		super("Testing buttons");
		setLayout( new FlowLayout() );

		textField1 = new JTextField( 10 );
		double s = Double.parseDouble(textField1.getText());
		add( textField1 );

		textField2 = new JTextField( 10 );
		textField2.setEditable(false);
		add( textField2 );

	result = s * kg;//Double.parseDouble(textField1.getText()); // * kg

		plainJButton = new JButton("Addition Button");
		add( plainJButton );

		ButtonHandler handler = new ButtonHandler();
		plainJButton.addActionListener( handler );
	}

	private class ButtonHandler implements ActionListener{
		public void actionPerformed( ActionEvent event ){

			JOptionPane.showMessageDialog(null, "The conversion is"+ result,
				"of a value", JOptionPane.PLAIN_MESSAGE);
		}
	}
}


This post has been edited by bartsimpsong: 28 March 2011 - 11:17 PM

Was This Post Helpful? 0
  • +
  • -

#7 Greltam  Icon User is offline

  • D.I.C Head

Reputation: 90
  • View blog
  • Posts: 225
  • Joined: 29-January 09

Re: adding two numbers

Posted 29 March 2011 - 03:30 AM

Quote

So my question is this; Since I have two textboxes I am not sure how to convert the first box to integer to use it for calculations, and output in the second box the result. My current code just pops up the window with two text boxes and a button but since I don't know how to convert the first box to int there is no calculations. Please any help is appreciated. Thank you


To convert a String to a primitive, you can use the wrapper classes(such as Integer, Double) to parse the String for valid values. An example would be
double d = Double.parseDouble(textField.getText());


This post has been edited by Greltam: 29 March 2011 - 03:31 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1