Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- import java.util.regex.*;
- public class Main {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- String input = scanner.nextLine();
- Pattern pattern = Pattern.compile("([|#])([A-Za-z\\s]+)\\1(\\d{2}/\\d{2}/\\d{2})\\1(\\d+)\\1");
- Matcher matcher = pattern.matcher(input);
- int totalCalories = 0;
- List<String> foodItems = new ArrayList<>();
- while (matcher.find()) {
- String itemName = matcher.group(2);
- String expirationDate = matcher.group(3);
- int calories = Integer.parseInt(matcher.group(4));
- totalCalories += calories;
- foodItems.add(String.format("Item: %s, Best before: %s, Nutrition: %d", itemName, expirationDate, calories));
- }
- int days = totalCalories / 2000;
- System.out.println("You have food to last you for: " + days + " days!");
- for (String item : foodItems) {
- System.out.println(item);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement