'How can I extract the floating number in a string in JAVA?

Hi I'm pretty new to java and I am writing this code.

for (int i = 0; i < length; i++) {
            comparator = str.charAt(i);
            if (comparator == '.') {
                dot_counter++;
            }
            if (comparator == '(') {
                list.add(tt_lparen);
            } else if (comparator == ')') {
                list.add(tt_rparen);
            } else if (comparator == '+') {
                list.add(tt_plus);
            } else if (comparator == '-') {
                list.add(tt_mul);
            } else if (comparator == '/') {
                list.add(tt_div);
            } else if (comparator == '*') {
                list.add(tt_mul);
            } else if (comparator == '*') {
                list.add(tt_minus);
            } else if (Character.isDigit(comparator)) {

                if (dot_counter == 0) {
                    list.add(tt_int + comparator);
                } else {
                    Pattern pattern = Pattern.compile("[-+]?[0-9]*\\.?[0-9]+");
                    Matcher matcher = pattern.matcher(str);
                    while (matcher.find()) {
                        list.add(tt_flaot + matcher.group());
                    }
                }

            }
        }
        System.out.println(list);
    }

Example. When you input: 1 + 1

The output should be [INT: 1, PLUS, INT: 1]

I already done the integer part

But I think What i done is wrong when I want to get a floating number (code reference above)

My goal is to input: 1.1 + 1.1 The end goal output is : [FLOAT: 1.1, PLUS, FLOAT: 1.1]

The output that im getting is: [FLOAT: 1.1, FLOAT: 1.1, PLUS, FLOAT: 1.1, FLOAT: 1.1, FLOAT: 1.1, FLOAT: 1.1]



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source