Hi I have a simple program I put together and it runs ok but I want it to "store the console input" from the first number, and subtract the second number from it showing the balance... JOptionPane comeup just fine and everything else works fine I just want it to store the numbers in the buffer and subtract num2 from num1 and show the remaining balance.... any help in this little problem is greatly appreciated.. NewGuy...
import java.awt.*; import javax.swing.*; import java.awt.event.*; class SubtractionOfNumbers{ public static void main(String[] args){ JFrame f=new JFrame(); JLabel label1=new JLabel("First Number: "); JLabel label2=new JLabel("Second Number: "); JLabel label3=new JLabel("Result: "); final JTextField text1=new JTextField(20); final JTextField text2=new JTextField(20); final JTextField text3=new JTextField(20); JButton button=new JButton("Calculate"); button.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ int num1=Integer.parseInt(text1.getText()); int num2=Integer.parseInt(text2.getText()); int result=0; if(num1>num2){ result=num1-num2; text3.setText(Integer.toString(result)); } else{ result=num2-num1; text3.setText(Integer.toString(result)); } } }); JPanel p=new JPanel(new GridLayout(4,2)); p.add(label1); p.add(text1); p.add(label2); p.add(text2); p.add(label3); p.add(text3); p.add(button); f.add(p); f.setVisible(true); f.pack(); } }
import java.awt.*; import javax.swing.*; import java.awt.event.*; class SubtractionOfNumbers{ public static void main(String[] args){ String number1=JOptionPane.showInputDialog(null,"Enter Number1: "); int num1=Integer.parseInt(number1); String number2=JOptionPane.showInputDialog(null,"Enter Number2: "); int num2=Integer.parseInt(number2); int result=0; if(num1>num2){ result=num1-num2; JOptionPane.showMessageDialog(null,"Result is: "+Integer.toString(result)); } else{ result=num2-num1; JOptionPane.showMessageDialog(null,"Result is: "+Integer.toString(result)); } } }