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;
- public class WinningTicketSimpleMethod {
- public static void main(String[] args) {
- Scanner console = new Scanner(System.in);
- String input = console.nextLine();
- String[] tickets = input.split("\\s*,\\s+");
- for (String ticket : tickets) {
- Pattern pattern = Pattern.compile("[\\\\@]{6,}|[\\\\$]{6,}|[\\\\#]{6,}|[\\\\^]{6,}");
- if (ticket.length() == 20) {
- String left = ticket.substring(0, 10);
- String right = ticket.substring(10, 20);
- String leftSide = "a";
- String rightSide = "s";
- Matcher leftMatch = pattern.matcher(left);
- Matcher rightMatch = pattern.matcher(right);
- if (leftMatch.find()){
- leftSide = leftMatch.group();
- }
- if (rightMatch.find()){
- rightSide = rightMatch.group();
- }
- if (leftSide.substring(0,1).equals(rightSide.substring(0,1))){
- int matchCount = Math.min(leftSide.length(), rightSide.length());
- if (matchCount == 10){
- System.out.printf("ticket \"%s\" - %d%s Jackpot!%n",ticket,matchCount, leftSide.substring(0,1));
- } else {
- System.out.printf("ticket \"%s\" - %d%s%n",ticket,matchCount, leftSide.substring(0,1));
- }
- }
- else {
- System.out.printf("ticket \"%s\" - no match%n",ticket);
- }
- }
- else {
- System.out.println("invalid ticket");
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment