
public class NumberObject {
public static void main(String[] args) {
NumberObject obj = new NumberObject();
obj.parseLong("hi");
obj.printOtherBase(1024);
}
public long parseLong(String data) {
//Long타입으로 전환하면 uncheckedException인 NumberFormatException이 발생할 수 있어서
// 이를 방지하고자 try catch로 묶어두었음
try {
return Long.parseLong(data);
} catch(NumberFormatException e) {
System.out.println(data + " is not a number.");
return -1;
}
}
public void printOtherBase(long value) {
String b = Long.toBinaryString(value);
String c = Long.toHexString(value);
String d = Long.toOctalString(value);
System.out.println("Original: " + value);
System.out.println("Binary: " + b);
System.out.println("Hex: " + c);
System.out.println("Octal " + d);
}
}
'Java' 카테고리의 다른 글
자바의 신 ch.22 collection - List (0) | 2022.05.23 |
---|---|
자바의 신 ch.21 제네릭 (0) | 2022.05.18 |
자바의신 1권 마지막 (0) | 2022.05.15 |
자바의 신 ch.16, 17 (0) | 2022.05.12 |
자바의 신 ch15 String (0) | 2022.05.11 |