diff --git a/Semester 2/Assignments/lab6-Exceptions_CalebFontenot/nb-configuration.xml b/Semester 2/Assignments/lab6-Exceptions_CalebFontenot/nb-configuration.xml
new file mode 100644
index 0000000..5b0f1f8
--- /dev/null
+++ b/Semester 2/Assignments/lab6-Exceptions_CalebFontenot/nb-configuration.xml
@@ -0,0 +1,18 @@
+
+
tags on this site."); @@ -44,8 +53,7 @@ public class ReadFileFromWeb1 { System.out.println(text); } catch (java.net.MalformedURLException ex) { System.out.println("Invalid URL"); - } - catch (java.io.IOException ex) { + } catch (java.io.IOException ex) { System.out.println("IO Errors!"); } } diff --git a/Semester 2/Assignments/lab6-Exceptions_CalebFontenot/src/main/java/com/mycompany/lab6/exceptions_calebfontenot/ToDecimal.java b/Semester 2/Assignments/lab6-Exceptions_CalebFontenot/src/main/java/com/mycompany/lab6/exceptions_calebfontenot/ToDecimal.java index 438c0db..5bf5ac4 100644 --- a/Semester 2/Assignments/lab6-Exceptions_CalebFontenot/src/main/java/com/mycompany/lab6/exceptions_calebfontenot/ToDecimal.java +++ b/Semester 2/Assignments/lab6-Exceptions_CalebFontenot/src/main/java/com/mycompany/lab6/exceptions_calebfontenot/ToDecimal.java @@ -11,33 +11,78 @@ import java.util.Scanner; * @author caleb */ public class ToDecimal { - public static int hexToDecimal(String hex) { - int decimalValue =0; + + 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) { + + 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 { + } 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) { + + /** + * + * @param binaryString the binary number to convert to decimal + * @return decimal number + * @throws NumberFormatException + * @throws NullPointerException + */ + public static int binToDecimal(String binaryString) throws NumberFormatException, NullPointerException + { + int returnInt = 0, binary = 1; + // Validate number first + if (binaryString == null) { + throw new NullPointerException("string is null"); + } + for (int i = 0; i < binaryString.length() - 1; ++i) { + if (binaryString.charAt(i) != '0' && binaryString.charAt(i) != '1') { + throw new NumberFormatException(binaryString + " is not a binary number!"); + } + } + binaryString = binaryString.trim(); + for (int i = binaryString.length() - 1; i >= 0; --i) { + //System.out.println("bin" + binaryString.charAt(i)); + if (binaryString.charAt(i) == '1') { + //System.out.println("Adding " + binary); + returnInt += binary; + } + binary *= 2; + //System.out.println(binary); + } + return returnInt; + } + + public static void main(String[] args) + { + try { + binToDecimal("Jeff Bezos"); + } catch (NumberFormatException ex) { + System.out.println(ex); + System.out.println("Binary number validation works!"); + } + System.out.println(binToDecimal("1010")); 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: "); + System.out.print("Enter a hex number: "); hex = input.nextLine(); } - + } }