ASDV-Java/Semester 2/Assignments/lab6-Exceptions_CalebFontenot/Printed HTMLs/ToDecimal.html
2023-03-21 11:43:38 -05:00

69 lines
4.8 KiB
HTML

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>ToDecimal.java</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<style type="text/css">
<!--
body {color: #a9b7c6; background-color: #2b2b2b; font-family: monospace}
pre {color: #a9b7c6; background-color: #2b2b2b; font-family: monospace}
table {color: #888888; background-color: #313335; font-family: monospace}
.number {color: #6897bb}
.string {color: #6a8759}
.comment {color: #808080}
.whitespace {color: #505050}
.ST0 {color: #808080; font-family: monospace; font-weight: bold}
.literal {color: #cc7832}
-->
</style>
</head>
<body>
<table width="100%"><tr><td align="center">/home/caleb/ASDV-Java/Semester 2/Assignments/lab6-Exceptions_CalebFontenot/src/main/java/com/mycompany/lab6/exceptions_calebfontenot/ToDecimal.java</td></tr></table>
<pre>
<span class="comment">/*</span>
<span class="comment"> * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license</span>
<span class="comment"> * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template</span>
<span class="comment"> */</span>
<span class="literal">package</span> com.mycompany.lab6.exceptions_calebfontenot;
<span class="literal">import</span> java.util.Scanner;
<span class="comment">/**</span>
<span class="comment"> *</span>
<span class="comment"> * </span><span class="ST0">@author</span> <span class="comment">caleb</span>
<span class="comment">*/</span>
<span class="literal">public</span> <span class="literal">class</span> ToDecimal {
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">int</span> hexToDecimal(String hex) {
<span class="literal">int</span> decimalValue =<span class="number">0</span>;
<span class="literal">for</span> (<span class="literal">int</span> i = <span class="number">0</span>; i &lt; hex.length(); ++i) {
<span class="literal">char</span> hexChar = hex.charAt(i);
decimalValue = decimalValue * <span class="number">16</span> + hexCharToDecimal(hexChar);
}
<span class="literal">return</span> decimalValue;
}
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">int</span> hexCharToDecimal(<span class="literal">char</span> ch) {
ch = Character.toUpperCase(ch);
<span class="literal">if</span> (ch &gt;= <span class="string">&#39;</span><span class="string">A</span><span class="string">&#39;</span> &amp;&amp; ch &lt;= <span class="string">&#39;</span><span class="string">F</span><span class="string">&#39;</span>) {
<span class="literal">return</span> <span class="number">10</span> + ch - <span class="string">&#39;</span><span class="string">A</span><span class="string">&#39;</span>;
} <span class="literal">else</span> <span class="literal">if</span> (ch &gt;= <span class="string">&#39;</span><span class="string">0</span><span class="string">&#39;</span> &amp;&amp; ch &lt;= <span class="string">&#39;</span><span class="string">9</span><span class="string">&#39;</span>){ <span class="comment">// ch is &#39;0&#39;, &#39;1&#39;, or &#39;9&#39;</span>
<span class="literal">return</span> ch - <span class="string">&#39;</span><span class="string">0</span><span class="string">&#39;</span>;
} <span class="literal">else</span> {
<span class="literal">throw</span> <span class="literal">new</span> NumberFormatException(<span class="string">&quot;</span><span class="string">Invalid hex character</span><span class="string">&quot;</span>);
}
}
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">void</span> main(String[] args) {
Scanner input = <span class="literal">new</span> Scanner(System.in);
System.out.print(<span class="string">&quot;</span><span class="string">Enter a hex number or q/Q to quit: </span><span class="string">&quot;</span>);
String hex = input.nextLine();
<span class="literal">while</span> (<span class="string">&quot;</span><span class="string">q</span><span class="string">&quot;</span>.equals(hex.toLowerCase()) == <span class="literal">false</span>) {
System.out.println(<span class="string">&quot;</span><span class="string">The decimal value for hex number </span><span class="string">&quot;</span> + hex + <span class="string">&quot;</span><span class="string"> is </span><span class="string">&quot;</span> + hexToDecimal(hex.toUpperCase()));
System.out.print(<span class="string">&quot;</span><span class="string">Emter a hex number: </span><span class="string">&quot;</span>);
hex = input.nextLine();
}
}
}
</pre></body>
</html>