By using our mini forum, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy

Q: SoftUni Defense System - Java Task

+8 votes

Mister Royal has a birthday party and the SoftUni team is busy drinking the contents of the fridge. People are swarming the place and each one of them is bringing more and more alcohol. Or at least, would like to bring. There is a slight problem – the SoftUni Defense System (SDS) is 80 years old and it’s not up to modern times, so all alcohol is immediately detected and destroyed.

The only way to get some booze to the university is if the alcohol name starts and ends with capital letters, and the quantity is in litters (denoted by L at the end, such as 12L).

The name of the guest should also start with a capital letter. All other letters in the guest and alcohol names must be lowercase.

So, you will be given lines of input such as Gosho$$$$RakiQ####12L. This will break the SDS, meaning that you print a message for everyone to know. The message should be Gosho brought 12 liters of rakiq! The input ends with the message OK KoftiShans. At that point, print the total amount of alcohol measured in the SoftUni Measurement System™. According to that system, 1 normal liter is 0.001 softuni litters.


  • Until the line OK KoftiShans is reached, you will be given lines of input, containing only ASCII symbols.


  • Print each match found on a new line in format {Guest} brought {quantity} liters of {drink}!
  • On the last line, print the total quantity in SoftUni Liters, formatted to three digits after the separator


  • The number of strings is in range [2 … 100]
  • Each string length is in range [1 … 100] and can contain any ASCII character.
  • The guest and alcohol names are at least two characters long and contain only English letters.
  • The alcohol quantity is an integer in range [1 … 1000]
  • Allowed memory/time: 16MB/0.25s


example 1 softuni defense system

example 2 softuni defense system

asked in Java category by user sam
edited by user golearnweb

1 Answer

+2 votes
Best answer

Nice task! :-)

For the regex you can use: - and for this task this particular regex:

Here's my solution:


import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Pr_02_SoftUniDefenseSystem_15November2015 {
    public static void main(String[] args) {

        Scanner console = new Scanner(;

        String line = console.nextLine();

        Pattern pattern = Pattern.compile("([A-Z][a-z]+).*?([A-Z][a-z]*[A-Z]).*?(\\d+)L");

        int total = 0;
        while (!line.equals("OK KoftiShans")) {
            Matcher matcher = pattern.matcher(line);
            while (matcher.find()) {
                String name =;
                String alcohol =;
                int quantity = Integer.parseInt(;

                System.out.printf("%s brought %d liters of %s!%n", name, quantity, alcohol);
                total += quantity;
            line = console.nextLine();
        System.out.printf("%.3f softuni liters", total / 1000d);
answered by user john7
edited by user golearnweb