emodev

WinningTicket

Nov 19th, 2018
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.79 KB | None | 0 0
  1. import java.util.Scanner;
  2. import java.util.regex.Matcher;
  3. import java.util.regex.Pattern;
  4.  
  5. public class WinningTicketSimpleMethod {
  6.     public static void main(String[] args) {
  7.         Scanner console = new Scanner(System.in);
  8.  
  9.         String input = console.nextLine();
  10.         String[] tickets = input.split("\\s*,\\s+");
  11.  
  12.         for (String ticket : tickets) {
  13.             Pattern pattern = Pattern.compile("[\\\\@]{6,}|[\\\\$]{6,}|[\\\\#]{6,}|[\\\\^]{6,}");
  14.  
  15.  
  16.             if (ticket.length() == 20) {
  17.  
  18.                 String left = ticket.substring(0, 10);
  19.                 String right = ticket.substring(10, 20);
  20.                 String leftSide = "a";
  21.                 String rightSide = "s";
  22.  
  23.                 Matcher leftMatch = pattern.matcher(left);
  24.                 Matcher rightMatch = pattern.matcher(right);
  25.                 if (leftMatch.find()){
  26.                     leftSide = leftMatch.group();
  27.                 }
  28.                 if (rightMatch.find()){
  29.                     rightSide = rightMatch.group();
  30.                 }
  31.  
  32.                 if (leftSide.substring(0,1).equals(rightSide.substring(0,1))){
  33.                     int matchCount = Math.min(leftSide.length(), rightSide.length());
  34.  
  35.                     if (matchCount == 10){
  36.                         System.out.printf("ticket \"%s\" - %d%s Jackpot!%n",ticket,matchCount, leftSide.substring(0,1));
  37.                     } else {
  38.                         System.out.printf("ticket \"%s\" - %d%s%n",ticket,matchCount, leftSide.substring(0,1));
  39.                     }
  40.  
  41.                 }
  42.                 else {
  43.                     System.out.printf("ticket \"%s\" - no match%n",ticket);
  44.                 }
  45.  
  46.             }
  47.             else {
  48.                 System.out.println("invalid ticket");
  49.             }
  50.  
  51.         }
  52.     }
  53. }
Add Comment
Please, Sign In to add comment