blooming8

Split Method of (x,y)

Apr 3rd, 2022 (edited)
790
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.52 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class Partenza
  4. {
  5.     public static void main (String[] args)
  6.     {
  7.  
  8.         String patterns[]  = {"^\\(\\d{1,3}\\,\\s\\d{1,3}\\)$", "^\\(\\d{1,3}\\,\\d{1,3}\\)$"};
  9.         Scanner scan = new Scanner(System.in);
  10.        
  11.         try
  12.         {
  13.             System.out.println("\nInserisci una stringa (x, y): ");
  14.             String input = scan.nextLine();
  15.             // PATTERN: (x, y)
  16.             if (input.matches(patterns[0]) || (input.matches(patterns[1])))
  17.             {
  18.                 Split.splitComma(input);
  19.             }
  20.             else
  21.             {
  22.                 throw new MyFormatException();
  23.             }
  24.         }
  25.         catch (MyFormatException | NumberFormatException exc)
  26.         {
  27.             System.out.println(exc.getMessage());
  28.         }
  29.         finally
  30.         {
  31.             scan.close();
  32.         }
  33.     }
  34. }
  35.  
  36.  
  37.  
  38.  
  39. public class Split
  40. {
  41.     public static void splitComma(String stringa) throws NumberFormatException
  42.     {
  43.         stringa = stringa.replace(" ", "").replace(")", "").replace("(", "");
  44.         String pezzi[] = stringa.split(",");
  45.         System.out.printf("\nX: %d\nY: %d\n", Integer.parseInt(pezzi[0]), Integer.parseInt(pezzi[1]));
  46.     }
  47. }
  48.  
  49.  
  50.  
  51. public class MyFormatException extends Exception
  52. {
  53.     private final String MESSAGE;
  54.  
  55.     MyFormatException()
  56.     {
  57.         this.MESSAGE = "\nErrore nel formato input.";
  58.     }
  59.    
  60.     @Override
  61.     public String getMessage()
  62.     {
  63.         return this.MESSAGE;
  64.     }  
  65. }
  66.  
Add Comment
Please, Sign In to add comment