Search code examples
javauser-interfacecalculatorgettext

Is there a way to seperate operators from number in a textfield when using getText()?


This is the whole code.

package helwrld;

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


public class SwingTrial {
    JButton b;
    private double num1;
    private double num2;
    private double result;
    private String opr = "";
    
    
    
    public static void main(String[] args) {
        SwingTrial gui= new SwingTrial();
        gui.go();
    }
    public void go() {
    JFrame box1= new JFrame();
    final JTextField tf=new JTextField(); 
    tf.setBounds(100,50, 150,20); 
    final JButton b= new JButton("1");
    b.setBounds(30,30,60,60);
    final JButton c= new JButton("2");
    c.setBounds(30,100,60,60);
    final JButton d= new JButton("+");
    d.setBounds(30,170,60,60);
    final JButton e= new JButton("=");
    e.setBounds(30,240,60,60);
    final JButton g= new JButton("3");
    g.setBounds(30,310,60,60);
    
    
    
    b.addActionListener(new ActionListener() {// listens for the "1" button being pressed, displays and stores the value in num1
        public void actionPerformed(ActionEvent arg0) {
            
            String eNum= tf.getText()+b.getText();
            tf.setText(eNum);
        }
    });
    
    c.addActionListener(new ActionListener() {// listens for the "2" button being pressed, displays and stores the value in num2
        public void actionPerformed(ActionEvent arg0) {
            
            String eNum= tf.getText()+c.getText();
            tf.setText(eNum);
        }
    });
    
    g.addActionListener(new ActionListener() {// listens for the "2" button being pressed, displays and stores the value in num2
        public void actionPerformed(ActionEvent arg0) {
            
            String eNum= tf.getText()+g.getText();
            tf.setText(eNum);
        }
    });
    d.addActionListener(new ActionListener() {// listens for the "+" button being pressed and displays it
        public void actionPerformed(ActionEvent arg0) {
            opr="+";
            num1= Double.valueOf(tf.getText());
            String eNum= tf.getText()+d.getText();
            tf.setText(eNum);
            
        }
    });
    
    
    e.addActionListener(new ActionListener() { //Adds the sums of the two nums and displays them next to the original numbers.
        public void actionPerformed(ActionEvent arg0) {
            String value= tf.getText();
            char aChar=value.charAt(2);
            num2= Double.valueOf(aChar);
            opr="=";
            if(opr.equals("=")) {
                result= num1+num2;
                
            }
            else if(opr.equals("+")) {
                tf.setText("Can't do that");
            }
            String addAnswer= String.format("%.1f", result); 
            String eNum= tf.getText()+e.getText()+addAnswer;
            
            tf.setText(eNum);
            
        }
    });
    
    
    
    box1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    box1.add(b);
    box1.add(c);
    box1.add(d);
    box1.add(e);
    box1.add(g);
    box1.add(tf);
    box1.setLayout(null);

    box1.setSize(500,500);
    box1.setVisible(true);

    }   
}

At first it was just,

String value= tf.getText();
            char aChar=value.charAt(2);
            num2= Double.parseDouble(tf.getText());
            opr="=";
            if(opr.equals("=")) {
                result= num1+num2;
                
            }
            else if(opr.equals("+")) {
                tf.setText("Can't do that");
            }
            String addAnswer= String.format("%.1f", result); 
            String eNum= tf.getText()+e.getText()+addAnswer;
            
            tf.setText(eNum);

But this gave me so many errors. So I tried converting the string value from getText() to a char and then a double, but its giving a wrong answer.

String value= tf.getText();
            char aChar=value.charAt(2);
            num2= Double.valueOf(aChar);
            opr="=";
            if(opr.equals("=")) {
                result= num1+num2;
                
            }
            else if(opr.equals("+")) {
                tf.setText("Can't do that");
            }
            String addAnswer= String.format("%.1f", result); 
            String eNum= tf.getText()+e.getText()+addAnswer;
            
            tf.setText(eNum);

Solution

  • I think that you will have to consider even multiple digits numbers (N > 9), float numbers and negative numbers (N < 0).

    I would use a regular expression to find in your string what can be considered a number.

    Here is an example but mind that:

    1. numbers will need to be separated by a whitespace
    2. you will have to iterate till the last match to find your number
    public static List<Double> getNumbersFromString(String input) {
        Pattern p = Pattern.compile("([+-]?\\d+\\.?\\d+)|([+-]?\\d+)");
        Matcher m = p.matcher(input);
        List<Double> doubleList = new ArrayList<>();
        while (m.find()) {
            doubleList.add(Double.parseDouble(m.group()));
        }
        return doubleList;
    }
    
    public static void main(String[] args) {
        String value = "1 + 2 - 30 * 5 / ( 3 + 4.3) + -120"; // tf.getText();
    
        // My suggestion
        List<Double> numbersFromString = getNumbersFromString(value);
        System.out.println(numbersFromString.get(numbersFromString.size() - 1));
    
        // Your code
        value = String.valueOf(value.charAt(value.length() - 1)); // gets the last char in _value_
        Double num2 = Double.parseDouble(value);
        System.out.println(num2);
    }