티스토리 뷰
import java.util.Scanner;
import java.util.regex.Pattern;
public class HexMain {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String number = scanner.nextLine();
String[] nn = number.split("");
boolean flag = Pattern.matches("^[a-fA-F0-9]*$", number);
try {
if(flag) {
System.out.println(Integer.parseInt(number, 16));
} else {
throw new HexException();
}
} catch(HexException e) {
e.printStackTrace();
}
}
}
class HexException extends Exception {
HexException(){
super();
System.out.println("hex가 아님");
}
}
hex를 입력했을 때 만약 입력한 숫자가 hex가 아니면 Exception을 throw 하고
맞다면 hex를 int로 바꾸어서 출력해준다
Integer.parseInt(number, 16)
이게 hex를 int로 바꿔줌
'개발일기' 카테고리의 다른 글
| [JAVA] 앞뒤가 같은 10진수 만들기 (0) | 2023.01.26 |
|---|---|
| [JAVA] 유클리드 호제법 (0) | 2023.01.26 |
| [Vue] data 변수 값 변경 감지 (0) | 2023.01.25 |
| [Vue] 상위 컴포넌트의 함수 실행 (0) | 2023.01.25 |
| [PostgreSql] 제약 조건 조회, 삭제 (does not exist 오류) (0) | 2023.01.25 |