import java.util.Scanner;
public class Exercise13_6{
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter a hex number: ");
String hex=input.nextLine();
System.out.println("The decimal value for hex number "
+ hex + " is " + hexToDecimal(hex.toUpperCase()));
}
public static int hexToDecimal(String hex) {
int decimalValue=0;
for(int i=0; i<hex.length();i++) {
char hexChar=hex.charAt(i);
decimalValue=decimalValue*16+hexToDecimal(hexChar);
}
return decimalValue;
}
public static int hexToDecimal(char ch) {
int temp=0;
try{
if (ch<'0' || ch>'F')
throw new NumberFormatException("not a hex number.");
if (ch>='A' && ch <='F')
temp = 10+ch-'A';
else
temp = ch-'0';
}catch (NumberFormatException ex){
// ex.printStackTrace();
}
return temp;
}
}
public static int hexToDecimal(char ch) throws NumberFormatException {
int temp=0;
if (ch<'0' || ch>'F')
throw new NumberFormatException("not a hex number.");
if (ch>='A' && ch <='F')
temp = 10+ch-'A';
else
temp = ch-'0';
return temp;
}
public class TryHexToDecimal {
public static int hexToDecimal(char ch) throws NumberFormatException {
int temp=0;
if (ch<'0' || ch>'F')
throw new NumberFormatException("not a hex number.");
if (ch>='A' && ch <='F')
temp = 10+ch-'A';
else
temp = ch-'0';
return temp;
}
public static void main(String[] args) {
System.out.println(hexToDecimal('G'));
}
}
Exception in thread "main" java.lang.NumberFormatException: not a hex number.
at Miscellaneous.hexToDecimal(Miscellaneous.java:24)
at Miscellaneous.main(Miscellaneous.java:39)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:110)
boolean good = false;
if((ch >= '0' && ch <= 'F') || (ch >= 'a' && ch <= 'f'))good = true;
if (!good)
throw new NumberFormatException("not a hex number.");
int inum = Integer.parseInt("12Fc10",16);
System.out.println("inum: " + inum);
inum: 1244176
int inum1 = Integer.parseInt("12Fc10g",16);
Exception in thread "main" java.lang.NumberFormatException: For input string: "12Fc10g"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)
at java.lang.Integer.parseInt(Integer.java:458)
at Miscellaneous.main(Miscellaneous.java:47)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:110)
ASKER
import java.util.Scanner;
public class Exercise13_6{
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter a hex number: ");
String hex=input.nextLine();
System.out.println("The decimal value for hex number "
+ hex + " is " + hexToDecimal(hex.toUpperCase()));
}
public static int hexToDecimal(String hex) {
int decimalValue=0;
for(int i=0; i<hex.length();i++) {
char hexChar=hex.charAt(i);
decimalValue=decimalValue*16+hexToDecimal(hexChar);
}
return decimalValue;
}
// public static int hexToDecimal(char ch) {
public static int hexToDecimal(char ch) throws NumberFormatException {
int temp=0;
if (ch<'0' || ch>'F')
throw new NumberFormatException(ch + " is not a hex number.");
if (ch>='A' && ch <='F')
temp = 10+ch-'A';
else
temp = ch-'0';
return temp;
}
}
public static int hexToDecimal(char ch) {
int temp=0;
try{
boolean good = false;
if((ch >= '0' && ch <= 'F') || (ch >= 'a' && ch <= 'f'))good = true;
if (!good)
throw new NumberFormatException("not a hex number.");
if (ch>='A' && ch <='F')
temp = 10+ch-'A';
else
temp = ch-'0';
} catch(Exception ex){
System.out.println("Not a hex");
}
return temp;
}
ASKER
pubblic class HandlingExcept {
public static int hexToDecimal(char ch) {
int temp=0;
try{
boolean good = false;
if((ch >= '0' && ch <= 'F') || (ch >= 'a' && ch <= 'f'))good = true;
if (!good)
throw new NumberFormatException("not a hex number.");
if (ch>='A' && ch <='F')
temp = 10+ch-'A';
else
temp = ch-'0';
} catch(Exception ex){
System.out.println(ch + " is not a hex character");
}
return temp;
}
public static void main(String[] args) {
System.out.println(hexToDecimal('g'));
}
}
g is not a hex character
ASKER
import java.util.Scanner;
public class Exercise13_6{
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter a hex number: ");
String hex=input.nextLine();
System.out.println("The decimal value for hex number "
+ hex + " is " + hexToDecimal(hex.toUpperCase()));
}
public static int hexToDecimal(String hex) {
int decimalValue=0;
for(int i=0; i<hex.length();i++) {
char hexChar=hex.charAt(i);
decimalValue=decimalValue*16+hexToDecimal(hexChar);
}
return decimalValue;
}
public static int hexToDecimal(char ch) throws
InvalidDataException13_6, NumberFormatException {
int temp=0;
boolean good = false;
try{
if((ch >= '0' && ch <= 'F') ||
(ch >= 'a' && ch <= 'f'))good = true;
if (!good) //{
throw new InvalidDataException13_6(ch+" is not a hex number.");
// }else{
if (ch>='A' && ch <='F')
temp = 10+ch-'A';
else
temp = ch-'0';
// }
} catch(Exception ex){
// System.out.println("Not a hex");
throw new InvalidDataException13_6 (ch+" is not a hex number.");
}
return temp;
}
}
class InvalidDataException13_6 extends Exception {
private String mistake;
public InvalidDataException13_6(String err)
{
super(err); // call super class constructor
mistake = err; // save message
}
public String toString(){
return mistake;
}
}
ASKER
import java.util.Scanner;
public class Exercise13_6{
public static void main(String[] args) throws
InvalidDataException13_6, NumberFormatException {
Scanner input = new Scanner(System.in);
System.out.print("Enter a hex number: ");
String hex=input.nextLine();
System.out.println("The decimal value for hex number "
+ hex + " is " + hexToDecimal(hex.toUpperCase()));
}
public static int hexToDecimal(String hex) throws
InvalidDataException13_6, NumberFormatException {
int decimalValue=0;
for(int i=0; i<hex.length();i++) {
char hexChar=hex.charAt(i);
decimalValue=decimalValue*16+hexToDecimal(hexChar);
}
return decimalValue;
}
public static int hexToDecimal(char ch) throws
InvalidDataException13_6, NumberFormatException {
int temp=0;
try{
if((ch >= '0' && ch <= 'F') || (ch >= 'a' && ch <= 'f'))//good = true;
throw new InvalidDataException13_6(ch+" is not a hex number.");
if (ch>='A' && ch <='F')
temp = 10+ch-'A';
else
temp = ch-'0';
} catch(Exception ex){
throw new InvalidDataException13_6 (ch+" is not a hex number.");
}
return temp;
}
}
class InvalidDataException13_6 extends Exception {
private String mistake;
public InvalidDataException13_6(String err)
{
super(err); // call super class constructor
mistake = err; // save message
}
public String toString(){
return mistake;
}
}
ASKER
ASKER
ASKER
boolean good = false;
if((ch >= '0' && ch <= 'F') || (ch >= 'a' && ch <= 'f'))good = true;
if (!good)
throw new NumberFormatException("not a hex number.");
ASKER
public static int hexToDecimal(char ch) {
int temp=0;
try{
boolean good = false;
if((ch >= '0' && ch <= 'F') || (ch >= 'a' && ch <= 'f'))good = true;
if (!good)
throw new NumberFormatException("not a hex number.");
if (Character.toUpperCase(ch)>='A' && Character.toUpperCase(ch)<='F')
temp = 10+Character.toUpperCase(ch)-'A';
else
temp = Character.toUpperCase(ch)-'0';
} catch(Exception ex){
System.out.println(ch + " is not a hex character");
}
return temp;
}
ch = Character.toLowerCase(ch);
good = "0123456789abcdef".indexOf(ch) > -1;
Java is a platform-independent, object-oriented programming language and run-time environment, designed to have as few implementation dependencies as possible such that developers can write one set of code across all platforms using libraries. Most devices will not run Java natively, and require a run-time component to be installed in order to execute a Java program.
TRUSTED BY
This works for me:
Open in new window