/home/caleb/ASDV-Java/Semester 2/Assignments/lab6-Exceptions_CalebFontenot/src/main/java/com/mycompany/lab6/exceptions_calebfontenot/ToDecimal.java
/*
 * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
 * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
 */
package com.mycompany.lab6.exceptions_calebfontenot;

import java.util.Scanner;

/**
 *
 * @author caleb
 */
public class ToDecimal {
    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 + hexCharToDecimal(hexChar);
        }
        return decimalValue;
    }
    public static int hexCharToDecimal(char ch) {
        ch = Character.toUpperCase(ch);
        if (ch >= 'A' && ch <= 'F') {
            return 10 + ch - 'A';
        } else if (ch >= '0' && ch <= '9'){ // ch is '0', '1', or '9'
                    return ch - '0';
               } else {
            throw new NumberFormatException("Invalid hex character");
        }
    }
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.print("Enter a hex number or q/Q to quit: ");
        String hex = input.nextLine();
        while ("q".equals(hex.toLowerCase()) == false) {
            System.out.println("The decimal value for hex number " + hex + " is " + hexToDecimal(hex.toUpperCase()));
            System.out.print("Emter a hex number: ");
            hex = input.nextLine();
        }
        
    }
}