还有一种方法:
---------------------------
异处理方法:
package qiuhe;
import *;
public class Untitled1 {
public static void main(String[] args) {
String s = "";
int sum = 0;
int i = 0;
System.out.println("please input a line");
try {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
s = in.readLine();
} catch (IOException e) {
e.printStackTrace();
}
int digitnum = 0;
for (i = 0; i < s.length(); i++) {
switch (s.charAt(i)) {
case '0':
digitnum = 0;
break;
case '1':
digitnum = 1;
break;
case '2':
digitnum = 2;
break;
case '3':
digitnum = 3;
break;
case '4':
digitnum = 4;
break;
case '5':
digitnum = 5;
break;
case '6':
digitnum = 6;
break;
case '7':
digitnum = 7;
break;
case '8':
digitnum = 8;
break;
case '9':
digitnum = 9;
break;
case 'A':
digitnum = 10;
break;
case 'B':
digitnum = 11;
break;
case 'C':
digitnum = 12;
break;
case 'D':
digitnum = 13;
break;
case 'E':
digitnum = 14;
break;
case 'F':
digitnum = 15;
break;
}
sum += digitnum * Math.pow(16, s.length() - i);
}
System.out.println("you have entered:" + sum);
}
}