0
package calc;

import java.awt.event.*;
import javax.swing.*;
import java.awt.*;

public class calc1 extends JFrame {

public JButton plus;
public JButton minus;
public JButton multiply;
public JButton divide;
public JTextField num1;
public JTextField num2;
public JTextField res;
public long numb1;
public long numb2;
public long result;
public String boob;
public String boob2;






public calc1(){


    setSize(400,400);
    setLayout(new FlowLayout());
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setVisible(true);

This is the Problem I'm trying to get the value from textfield 'num1' to turn it into an int/long/double so I can use it in math functions in my listeners bellow.

    num1 = new JTextField("Enter First Number:");
    boob = num1.getText();
    numb1 = Long.parseLong(boob);
    add(num1);

This is the Problem I'm trying to get the value from textfield 'num1' to turn it into an int/long/double so I can use it in math functions in my listeners bellow.

    num2 = new JTextField("Enter Second Number:");
    boob2 = num2.getText();
    numb2 = Long.parseLong(boob2);
    add(num2);



    plus = new JButton("+");
    add(plus);

    minus = new JButton("-");
    add(minus);

    multiply = new JButton("*");
    add(multiply);

    divide = new JButton(":");
    add(divide);

    listener1 boo1 = new listener1();
    listener2 boo2 = new listener2();
    listener3 boo3 = new listener3();
    listener4 boo4 = new listener4();




    divide.addActionListener(boo1);
    multiply.addActionListener(boo2);
    minus.addActionListener(boo3);
    plus.addActionListener(boo4);




    }

public class listener1 implements ActionListener{
    public void actionPerformed(ActionEvent boo1){


        result = numb1/numb2;
        String ahmood = String.valueOf(result);
        res = new JTextField(ahmood);
        add(res);


    }

}

public class listener2 implements ActionListener{
    public void actionPerformed(ActionEvent boo2){


        result = numb1*numb2;
        String ahmood = String.valueOf(result);
        res = new JTextField(ahmood);
        add(res);




    }

}

public class listener3 implements ActionListener{
    public void actionPerformed(ActionEvent boo3){

        result = numb1-numb2;
        String ahmood = String.valueOf(result);
        res = new JTextField(ahmood);
        add(res);

    }

}

public class listener4 implements ActionListener{
    public void actionPerformed(ActionEvent boo4){

        result = numb1+numb2;
        String ahmood = String.valueOf(result);
        res = new JTextField(ahmood);
        add(res);


    }

}

public static void main(String args[]){

    calc1 joss = new calc1();
    joss.setVisible(true);


}

}

I've tried many ways, and I ended up with many redundant variables. Heres the error I keep getting.

Exception in thread "main" java.lang.NumberFormatException: For input string: "Enter First Number:"
    at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
    at java.lang.Long.parseLong(Long.java:589)
    at java.lang.Long.parseLong(Long.java:631)
    at calc.calc1.<init>(calc1.java:37)
    at calc.calc1.main(calc1.java:135)

1 Answers1

0

You create input text fields initialized with value "Enter some Number:", then immediately proceed to get the value (hint: the one you just passed), and convert it to a Long. Of course that will throw a NumberFormatException.

You should do 2 things:

  • (facultative) put that message in a JLabel next to the input field.
  • (mandatory) do not get value of input and convert it right after creating the input field, that makes no sense. Keep that part for your listeners.

If you still can't find your way with that, try a smaller program with just one text field, make it work, and build on that.

Hugues M.
  • 19,846
  • 6
  • 37
  • 65