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