우아한 프로그래밍


 import java.util.Scanner;

 

public class Main {

 

        public static Scanner sc = new Scanner(System.in);

 

        // 표준입력을 사용하는 클라이언트

        public static void main(String[] args) throws Exception {

              

               // 모든문자

               System.out.print("입력 : ");

               Console.read( Console.ALL_CHARACTERS_WITH_SPECIAL);

              

               // 모든문자에서 공백 추가

               System.out.print("입력 : ");                       

               Console.read( Console.enableOption(Console.ALL_CHARACTERS_WITH_SPECIAL, Console.ONLY_WHITE_SPACE));

              

               // 모든 문자에서 특수문자 제거

               System.out.print("입력 : ");

               Console.read( Console.disableOption(Console.ALL_CHARACTERS_WITH_SPECIAL, Console.ONLY_SPECIAL_CHARACTERS));


 import java.util.Scanner;

 

public class Console {

 

        private static Scanner scanner = new Scanner(System.in);

 

        // 숫자

        public static final int ONLY_INTEGER = 0x0000000001;

        // 대문자

        public static final int ONLY_BIG_ALPHABET = 0x0000000002;

        // 소문자

        public static final int ONLY_SMALL_ALPHABET = 0x0000000004;

        // 한글

        public static final int ONLY_KOREAN = 0x0000000008;

        // 특수문자

        public static final int ONLY_SPECIAL_CHARACTERS = 0x0000000010;

        // 공백만

        public static final int ONLY_WHITE_SPACE = 0x0000000020;

 

        // 대문자 + 소문자

        public static final int ONLY_ALPHABET = ONLY_BIG_ALPHABET | ONLY_SMALL_ALPHABET;

        // 대문자 + 소문자 + 한글

        public static final int ONLY_CHARACTERS = ONLY_ALPHABET | ONLY_KOREAN;

        // 대문자 + 소문자 + 한글 + 숫자

        public static final int ALL_CHARACTERS = ONLY_CHARACTERS | ONLY_INTEGER;

        // 대문자 + 소문자 + 한글 + 숫자 + 특수문자

        public static final int ALL_CHARACTERS_WITH_SPECIAL = ALL_CHARACTERS | ONLY_SPECIAL_CHARACTERS;

 

       

        // [0-9] 숫자만

        // [A-Z] 대문자

        // [a-z] 소문자

        // [ㄱ-ㅎ가-힣] 한글만

        // [\\s] 공백만

        // [~!@\#$%<>^&*\()\-=+_\’] 특수문자만

        public static String read( int options) throws Exception {

              

               StringBuffer regexp = new StringBuffer();

               String input = null;

              

               if( hasOption(options, ONLY_INTEGER)) {

                       regexp.append( "0-9");

               }

              

               if( hasOption(options, ONLY_BIG_ALPHABET)) {

                       regexp.append( "A-Z");

               }

              

               if( hasOption(options, ONLY_SMALL_ALPHABET)) {

                       regexp.append( "a-z");

               }

              

               if( hasOption(options, ONLY_SPECIAL_CHARACTERS)) {

                       regexp.append("~!@\\#$%<>^&*\\()\\-=+_\\’");

               }

              

               if( hasOption(options, ONLY_CHARACTERS)) {

                       regexp.append( "ㄱ-ㅎ가-힣");

               }

              

               if( hasOption(options, ONLY_WHITE_SPACE)) {

                       regexp.append( "\\s");

               }

                                            

               input = scanner.nextLine();

               if(  !input.matches(  String.format("[%s]+", regexp.toString()))) {

                       throw new Exception("Input values ​​do not match.");

               }

                                                                   

               return input;

        }

 

        // 옵션추가

        public static int enableOption(int options, int option) {

               return options | option;

        }

 

        // 옵션제거

        public static int disableOption(int options, int option) {

               return options & ~option;

        }

 

        // 옵션을 가지고 있는지 확인

        public static boolean hasOption(int options, int option) {

               return (options & option) != 0;

        }

 

}

 


profile

우아한 프로그래밍

@자바조아!

포스팅이 좋았다면 "좋아요❤️" 또는 "구독👍🏻" 해주세요!