-2

How do I write code to get text from JTextField and convert to a double?

I have created a bank account class and a bank account GUI with amountField to show the amount i wish to withdraw and deposit. Using public void actionPerformed(ActionEvent e) method how can I write code to get text from amountField and convert to a double?

I want to input account details and be able to withdraw and deposit whilst storing values within a string

  • write event handler for deposit button
  • write event handler for withdraw button

public class BankAccountGUI extends JFrame implements ActionListener
{
    private Label amountLabel = new Label("Amount");
    private JTextField amountField = new JTextField(5);
    private JButton depositButton = new JButton("DEPOSIT");
    private JButton withdrawButton = new JButton("WITHDRAW");
    private Label balanceLabel = new Label("Starting Balance = 0" );

    private JPanel topPanel = new JPanel();
    private JPanel bottomPanel = new JPanel();
    private JPanel middlePanel = new JPanel();


    BankAccount myAccount = new BankAccount("James","12345");

    // declare a new BankAccount object (myAccount) with account number and name of your choice here


    public BankAccountGUI()
    {
        setTitle("BankAccount GUI");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        setSize(340, 145);
        setLocation(300,300);
        depositButton.addActionListener(this);
        withdrawButton.addActionListener(this);

        topPanel.add(amountLabel);
        topPanel.add(amountField);
        bottomPanel.add(balanceLabel);
        middlePanel.add(depositButton);
        middlePanel.add(withdrawButton);


        add (BorderLayout.NORTH, topPanel);
        add(BorderLayout.SOUTH, bottomPanel);
        add(BorderLayout.CENTER, middlePanel);

        setVisible(true);
    }


    public void actionPerformed(ActionEvent e)
    {
         //What goes here?
    }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • Its best to share your own research on what you tried and what didn't work. – Nikhil Sahu Jul 16 '19 at 20:39
  • I dont actually have a clue of how to get text from the amount field – NostackJay Jul 16 '19 at 20:46
  • 1
    Possible duplicate of [How to Retrieve value from JTextField in Java Swing?](https://stackoverflow.com/questions/5752307/how-to-retrieve-value-from-jtextfield-in-java-swing) – Nikhil Sahu Jul 16 '19 at 21:01
  • For future reference, try to keep questions to one per post. You're asking two questions here: How to get the value out of a JTextField, and How to convert a String to a Double. It's also highly encouraged to try searching for your question prior to posting. Especially with Java, the odds of finding something useful here are very high. Happy Coding! – Stephan Jul 16 '19 at 21:05

2 Answers2

1

You are probably going to want to try using Double.parseDouble(string). for example:

String str = "12.0";
Double e = Double.parseDouble(str);
        System.out.println(e * 1.5); //returns 18  
  • https://www.journaldev.com/18392/java-convert-string-to-double you can see more examples here – Dylan Graham Jul 16 '19 at 20:52
  • This answers the question only partially and even the important bit. Moreover, answers to non-reasearched questions are highly discouraged. https://stackoverflow.com/help/how-to-ask – Nikhil Sahu Jul 16 '19 at 21:00
0

There's a number of things here you need to do differently. In a nutshell, you're looking for 'JTextField.getText()', and 'Double.parseDouble()'

depositButton.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
        //Get the Text Value out of the UI Component
        String rawAmount=amountField.getText();

        //Convert that value to Double
        try{
            Double convertedValue = Double.parseDouble(rawAmount);
            //Do stuff with the convertedValue
        }catch(Exception e){
            System.out.println("Not A Double Value");
        }
    }
});

You should also use separate listeners for each button to save you the trouble of trying to programmatically determine which button generated the event.

See the documentation for JTextField

Community
  • 1
  • 1
Stephan
  • 666
  • 8
  • 23
  • Thanks for the reply. But can you explain how this would store the input to a string. It’s all confusing for me to understand at the moment – NostackJay Jul 16 '19 at 22:47
  • If you look at the line where I call getText() : that's called an assignment operation. GetText is returning the string out of the field, which is then stored in the variable declared to the left of the = – Stephan Jul 16 '19 at 22:49