12.6
import java.util.Scanner;
public class NumberformatexceptionException12_6 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter a hex number: ");
String hex = input.nextLine();
hexToDec(hex.toUpperCase());
}
public static void hexToDec(String hex){
int num = 0;
try{
for(int i = 0;i < hex.length(); ++i){
char hexChar = hex.charAt(i);
if(hexChar >= '0' && hexChar <= '9'){
num = num * 16 + hexChar - '0';
}else if( hexChar >= 'A' && hexChar <= 'F'){
num = num * 16 + hexChar - 'A' + 10;
}else{
throw new Exception("NumberFormatException");
}
}
System.out.println("The decimal value for hex number " + hex + " is " + num);
}
catch (Exception e){
System.out.println(e.getMessage());
}
}
}
12.7
import java.util.Scanner;
public class NumberformatexceptionException12_7 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter a hex number: ");
String hex = input.nextLine();
bin2Dec(hex);
}
public static void bin2Dec(String hex){
int num = 0;
try{
for(int i = 0;i < hex.length(); ++i){
char binChar = hex.charAt(i);
if(binChar == '1' || binChar == '0'){
num = num * 2 + binChar - '0';
}
else{
throw new Exception("NumberFormatException");
}
}
System.out.println("The decimal value for bin number " + hex + " is " + num);
}
catch (Exception e){
System.out.println(e.getMessage());
}
}
}
12.8
import java.util.Scanner;
class HexFormatException extends Exception{
HexFormatException(String msg){
super(msg);
}
}
public class HexformatexceptionException12_8 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter a hex number: ");
String hex = input.nextLine();
hexToDec(hex.toUpperCase());
}
public static void hexToDec(String hex){
int num = 0;
try{
for(int i = 0;i < hex.length(); ++i){
char hexChar = hex.charAt(i);
if(hexChar >= '0' && hexChar <= '9'){
num = num * 16 + hexChar - '0';
}else if( hexChar >= 'A' && hexChar <= 'F'){
num = num * 16 + hexChar - 'A' + 10;
}else{
throw new HexFormatException("It isn't a hex number!");
}
}
System.out.println("The decimal value for hex number " + hex + " is " + num);
}
catch (Exception e){
System.out.println(e.getMessage());
}
}
}
12.9
import java.util.Scanner;
class BianryFormatException extends Exception{
BianryFormatException(String msg){
super(msg);
}
}
public class BinaryformatexceptionException12_9 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter a hex number: ");
String hex = input.nextLine();
bin2Dec(hex);
}
public static void bin2Dec(String hex){
int num = 0;
try{
for(int i = 0;i < hex.length(); ++i){
char binChar = hex.charAt(i);
if(binChar == '1' || binChar == '0'){
num = num * 2 + binChar - '0';
}
else{
throw new BianryFormatException("It isn't a bin number!");
}
}
System.out.println("The decimal value for bin number " + hex + " is " + num);
}
catch (Exception e){
System.out.println(e.getMessage());
}
}
}
12.10
import sun.awt.windows.WPrinterJob;
import java.util.Scanner;
public class OutofmemoryerrorError12_10 {
//给数组存的长度设置为int的最大值,就会引起爆栈
public static void main(String[] args) {
try{
int maxL = Integer.MAX_VALUE;
int a[] = new int[maxL];
System.out.println(maxL);
}catch (OutOfMemoryError e){
System.out.println("OutOfMemoryError!");
}
}
}