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 @@ + + + + + + true + + diff --git a/Semester 2/Assignments/lab6-Exceptions_CalebFontenot/nbactions.xml b/Semester 2/Assignments/lab6-Exceptions_CalebFontenot/nbactions.xml new file mode 100644 index 0000000..a3ff3a8 --- /dev/null +++ b/Semester 2/Assignments/lab6-Exceptions_CalebFontenot/nbactions.xml @@ -0,0 +1,55 @@ + + + + run + + jar + + + process-classes + org.codehaus.mojo:exec-maven-plugin:3.0.0:exec + + + -enableassertions + ${exec.vmArgs} -classpath %classpath ${exec.mainClass} ${exec.appArgs} + + ${packageClassName} + java + + + + debug + + jar + + + process-classes + org.codehaus.mojo:exec-maven-plugin:3.0.0:exec + + + -enableassertions -agentlib:jdwp=transport=dt_socket,server=n,address=${jpda.address} + ${exec.vmArgs} -classpath %classpath ${exec.mainClass} ${exec.appArgs} + + ${packageClassName} + java + true + + + + profile + + jar + + + process-classes + org.codehaus.mojo:exec-maven-plugin:3.0.0:exec + + + -enableassertions + ${exec.vmArgs} -classpath %classpath ${exec.mainClass} ${exec.appArgs} + ${packageClassName} + java + + + + diff --git a/Semester 2/Assignments/lab6-Exceptions_CalebFontenot/src/main/java/com/mycompany/lab6/exceptions_calebfontenot/ReadFileFromWeb1.java b/Semester 2/Assignments/lab6-Exceptions_CalebFontenot/src/main/java/com/mycompany/lab6/exceptions_calebfontenot/ReadFileFromWeb1.java index 54cd377..1e9ca42 100644 --- a/Semester 2/Assignments/lab6-Exceptions_CalebFontenot/src/main/java/com/mycompany/lab6/exceptions_calebfontenot/ReadFileFromWeb1.java +++ b/Semester 2/Assignments/lab6-Exceptions_CalebFontenot/src/main/java/com/mycompany/lab6/exceptions_calebfontenot/ReadFileFromWeb1.java @@ -11,20 +11,29 @@ import java.util.Scanner; * @author caleb */ public class ReadFileFromWeb1 { - public static void main(String[] args) { + + public static void main(String[] args) + { System.out.print("Enter a URL (defaults to \"https://calebfontenot.com\"): "); - String URLinput = new Scanner(System.in).nextLine(); - String URLString; - if (URLinput.isEmpty()) { - URLString = "https://calebfontenot.com"; - } - else { - URLString = URLinput; - } + String URLinput; + String URLString; try { + URLinput = new Scanner(System.in).nextLine(); + URLinput = ""; + if (URLinput.isEmpty()) { + URLString = "https://calebfontenot.com"; + } else { + URLString = URLinput; + } + try { + } catch (Exception ex) { + System.out.println(ex); + URLString = "https://calebfontenot.com"; + } + java.net.URL url = new java.net.URL(URLString); int count = 0, divCount = 0, pCount = 0; - String text =""; + String text = ""; Scanner input = new Scanner(url.openStream()); while (input.hasNext()) { String line = input.nextLine(); @@ -36,7 +45,7 @@ public class ReadFileFromWeb1 { if (line.contains(" 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(); } - + } }