-1
String quantity = cboAmount.getSelectedItem().toString();
String product = cboProduct.getSelectedItem().toString();

String price = product.replaceAll("(\\d+(?:\\.\\d{1,2})?)", "");

textareaOrder.setText(price);

I want to extract the price from some text in a combobox, I've googled people's solutions but they all didn't work, the one in the example displays everything but the price. I don't know if I'm missing something here. The text in the combobox is written like this "Orange: 2.00" if that matters.

Tried to extract price from a string but didn't get it

SelVazi
  • 10,028
  • 2
  • 13
  • 29

1 Answers1

-1

note: Use regex to capture the prices

Java Regex Pattern

import java.util.regex.*;

public class Main {
    public static void main(String[] args) {
        String priceString = "Orange: 2.00";
        Pattern pattern = Pattern.compile("\\d+\\.\\d+");
        Matcher m = pattern.matcher(priceString);
        if (m.find()) {
            System.out.println(m.group(0));
        }
    }
}
DT.rw
  • 11
  • 3
  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community May 12 '23 at 05:32