Home how to getting numbers from calculator text area(without operators)?
Reply: 2

how to getting numbers from calculator text area(without operators)?

mhamad arsalan
1#
mhamad arsalan Published in 2017-12-04 18:09:55Z

in my previous question, i got no useful answer, i hope i get something :)

here's my code:

import java.util.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

class Calculator extends JFrame
{
  public Calculator()
{
    JFrame jf = new JFrame("Calculator");
    jf.setLocation(500,200);
    jf.setSize(600,400);
    jf.setDefaultCloseOperation(jf.EXIT_ON_CLOSE);


    /*=======================Add panel==============================
    ================================================================*/
    JPanel jp = new JPanel();
    jp.setLayout(null);
    jp.setBackground(Color.cyan);
    jf.add(jp);

    /*========================Add Textarea========================
    ================================================================*/

    JTextArea jta = new JTextArea("");
    jta.setBounds(100,25,290,50);
    jp.add(jta);

    /*========================Creating Font========================
    ===============================================================*/

    Font labels = new Font("Lucida Console",Font.PLAIN, 20);
    Font nums = new Font("Lucida Console",Font.PLAIN, 9);

    /*========================Add Labels============================
    ================================================================*/

    /*  for H align: ,SwingConstants.CENTER */
    JLabel jlbl = new JLabel("=");
    jlbl.setFont(labels);   //  MAKING FONT BIGGER
    jlbl.setBounds(400,30,50,40);
    jp.add(jlbl);


    JLabel jlbl_result = new JLabel();
    String temp = jta.getText();
    jlbl_result.setText(temp);

    /*=========================Adding button============================
    ====================================================================*/

    JButton jbtn1 = new JButton("1");
    jbtn1.setBounds(120,100,50,40);
    jbtn1.setBackground(Color.green);
    jp.add(jbtn1);

    JButton jbtn2 = new JButton("2");
    jbtn2.setBounds(180,100,50,40);
    jbtn2.setBackground(Color.green);
    jp.add(jbtn2);

    JButton jbtn3 = new JButton("3");
    jbtn3.setBounds(240,100,50,40);
    jbtn3.setBackground(Color.green);
    jp.add(jbtn3);

    JButton jbtn4 = new JButton("4");
    jbtn4.setBounds(120,155,50,40);
    jbtn4.setBackground(Color.green);
    jp.add(jbtn4);

    JButton jbtn5 = new JButton("5");
    jbtn5.setBounds(180,155,50,40);
    jbtn5.setBackground(Color.green);
    jp.add(jbtn5);

    JButton jbtn6 = new JButton("6");
    jbtn6.setBounds(240,155,50,40);
    jbtn6.setBackground(Color.green);
    jp.add(jbtn6);

    JButton jbtn7 = new JButton("7");
    jbtn7.setBounds(120,210,50,40);
    jbtn7.setBackground(Color.green);
    jp.add(jbtn7);

    JButton jbtn8 = new JButton("8");
    jbtn8.setBounds(180,210,50,40);
    jbtn8.setBackground(Color.green);
    jp.add(jbtn8);

    JButton jbtn9 = new JButton("9");
    jbtn9.setBounds(240,210,50,40);
    jbtn9.setBackground(Color.green);
    jp.add(jbtn9);

    JButton jbtn0 = new JButton("0");
    jbtn0.setBounds(180,265,50,40);
    jbtn0.setBackground(Color.green);
    jp.add(jbtn0);

    JButton jbtn_sum = new JButton("+");
    jbtn_sum.setBounds(380,100,40,40);
    jbtn_sum.setFont(nums);
    jbtn_sum.setBackground(Color.gray);
    jp.add(jbtn_sum);

    JButton jbtn_sub = new JButton("-");
    jbtn_sub.setBounds(380,100,40,40);
    jbtn_sub.setFont(nums);
    jbtn_sub.setBackground(Color.gray);
    jp.add(jbtn_sub);

    JButton jbtn_equal = new JButton("-");
    jbtn_equal.setBounds(380,155,40,40);
    jbtn_equal.setFont(nums);
    jp.add(jbtn_equal);


    jf.setVisible(true);

            jbtn1.addActionListener(e->jta.append("1"));
            jbtn2.addActionListener(e->jta.append("2"));
            jbtn3.addActionListener(e->jta.append("3"));
            jbtn4.addActionListener(e->jta.append("4"));
            jbtn5.addActionListener(e->jta.append("5"));
            jbtn6.addActionListener(e->jta.append("6"));
            jbtn7.addActionListener(e->jta.append("7"));
            jbtn8.addActionListener(e->jta.append("8"));
            jbtn9.addActionListener(e->jta.append("9"));
            jbtn0.addActionListener(e->jta.append("0"));
            jbtn_equal.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent evt2)
                {

                }
            });
            //jbtn_sub.addActionListener(e->jta.append("-"));
            jbtn_sum.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent evt)
                {

                }
            });
}
public static void main(String...ser)
{
    new Calculator();
}
}

i know! there is a lot of code, i want to put number that i entered to JTextArea inside variable, how to get numbers(just numbers without operators ) and assign it to variable?

and... how many variable is required to do it?(in case if i have more than 5 variable)

and a lot of questions..., i cant write it down here

thank you for help(forgive me of my bad grammar)

PatrycjaMirko
2#
PatrycjaMirko Reply to 2017-12-04 18:27:20Z

You download the text from JTextArea and save it to the new String. If you download only numbers, just use the parseInt () function to convert String to int. If you enter an action with operators eg 2 + 2 then you can use patterns to capture numbers, you can use the split () function to separate 2 numbers in the String with some sign, the possibilities are many;)

If this is not what you had in mind, compose the question and I will try to help.

Keara
3#
Keara Reply to 2017-12-04 18:38:03Z

Get the text from the JTextArea and then split it by non-number characters into an array of Strings, like this:

String[] numbers = jta.getText().split("\\D+");

For example, if you used this regex to split the string "1+23-5" you would get this array: {"1","23","5"}.

If you want to store the numbers as ints, use a for-each loop to copy the String array into an int array:

int[] ints = new int[numbers.length]
for( int i = 0; i < numbers.length; i++){
    ints[i] = Integer.parseInt(numbers[i]);
}

Then, instead of putting each number in a separate variable, just keep them in the array and access them by index when needed.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.380029 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO