SHARE
TWEET

regex

a guest Jul 18th, 2019 72 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package com.company;
  2.  
  3. import java.util.*;
  4. import java.util.regex.Matcher;
  5. import java.util.regex.Pattern;
  6.  
  7. public class Main {
  8.  
  9.     public static void main(String[] args) {
  10.         Scanner sc = new Scanner(System.in);
  11.  
  12.         String input = sc.nextLine();
  13.  
  14.         List<String> things = new ArrayList<>();
  15.         double sum = 0;
  16.         while (!input.equals("Purchase")) {
  17.             Pattern pattern = Pattern.compile(">{2}([A-Za-z]+)<{2}([0-9]+.[0-9]+)!([0-9])");
  18.             Matcher matcher = pattern.matcher(input);
  19.  
  20.             if (matcher.find()) {
  21.                 String item = matcher.group(1);
  22.                 double price = Double.parseDouble(matcher.group(2));
  23.                 int quantity = Integer.parseInt(matcher.group(3));
  24.                 sum += price * quantity;
  25.                 things.add(item);
  26.  
  27.             }
  28.  
  29.             input = sc.nextLine();
  30.         }
  31.         System.out.println("Bought furniture:");
  32.  
  33.         for (int i = 0; i < things.size(); i++) {
  34.             System.out.println(things.get(i));
  35.         }
  36.         System.out.printf("Total money spend: %.2f", sum);
  37.  
  38.  
  39.     }
  40.  
  41. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top