Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.LinkedHashMap;
- import java.util.Map;
- import java.util.Scanner;
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
- public class Ex2 {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- String input = scanner.nextLine();
- String regex = "([#]|[|])(?<item>[A-Za-z\\s]+)\\1(?<date>\\d{2}\\/\\d{2}\\/\\d{2})\\1(?<calories>\\d{1,5})\\1";
- Pattern pattern = Pattern.compile(regex);
- Matcher matcher = pattern.matcher(input);
- Map<String, String> hoho = new LinkedHashMap<>();
- Map<String, Integer> ho = new LinkedHashMap<>();
- int calCount = 0;
- while (matcher.find()) {
- String food = matcher.group("item");
- String expire = matcher.group("date");
- int calories = Integer.parseInt(matcher.group("calories"));
- calCount = calCount + calories;
- hoho.put(food, expire);
- ho.put(food, calories);
- }
- int calPerDay = calCount / 2000;
- System.out.println(String.format("You have food to last you for: %d days!", calPerDay));
- hoho
- .forEach((key, value) -> System.out.printf("Item: %s, Best before %s, Nutrition: %d%n",
- key, value, ho.get()));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement