Regex Tutorial

mail@pastecode.io avatar
unknown
java
a month ago
949 B
3
Indexable
Never
public class Test {
    public static void main(String[] args) {
        String paragraph = "A core i 7 laptop price is 85000 tk " +
                "and a gaming mouse price is 2500 tk. If I buy the " +
                "laptop and 1 piece mouse, what will be my total cost " +
                "after giving 15% discount?";

        paragraph = paragraph.replaceAll("[^\\d]", " ").trim();
        paragraph = paragraph.replaceAll(" +", " ");

        String[] words = paragraph.split(" ");

        int laptopPrice = Integer.parseInt(words[1]);
        int mousePrice = Integer.parseInt(words[2]);
        double discountRate = Integer.parseInt(words[4]) / 100.0;

        //calculation
        int totalPrice = laptopPrice + mousePrice;
        double discount = totalPrice * discountRate;
        double finalPrice = totalPrice - discount;

        System.out.println("Price after discount : " + finalPrice + " Tk");
    }
}
Leave a Comment