Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class Partenza
- {
- public static void main (String[] args)
- {
- String patterns[] = {"^\\(\\d{1,3}\\,\\s\\d{1,3}\\)$", "^\\(\\d{1,3}\\,\\d{1,3}\\)$"};
- Scanner scan = new Scanner(System.in);
- try
- {
- System.out.println("\nInserisci una stringa (x, y): ");
- String input = scan.nextLine();
- // PATTERN: (x, y)
- if (input.matches(patterns[0]) || (input.matches(patterns[1])))
- {
- Split.splitComma(input);
- }
- else
- {
- throw new MyFormatException();
- }
- }
- catch (MyFormatException | NumberFormatException exc)
- {
- System.out.println(exc.getMessage());
- }
- finally
- {
- scan.close();
- }
- }
- }
- public class Split
- {
- public static void splitComma(String stringa) throws NumberFormatException
- {
- stringa = stringa.replace(" ", "").replace(")", "").replace("(", "");
- String pezzi[] = stringa.split(",");
- System.out.printf("\nX: %d\nY: %d\n", Integer.parseInt(pezzi[0]), Integer.parseInt(pezzi[1]));
- }
- }
- public class MyFormatException extends Exception
- {
- private final String MESSAGE;
- MyFormatException()
- {
- this.MESSAGE = "\nErrore nel formato input.";
- }
- @Override
- public String getMessage()
- {
- return this.MESSAGE;
- }
- }
Add Comment
Please, Sign In to add comment