ASDV-Java/Semester 2/Assignments/lab7_CalebFontenot/Printed HTMLs/occurrencesOfEachCharacter.html
2023-04-14 01:01:58 -05:00

76 lines
5.5 KiB
HTML

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>occurrencesOfEachCharacter.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; font-weight: bold}
pre {color: #a9b7c6; background-color: #2b2b2b; font-family: monospace; font-weight: bold}
table {color: #888888; background-color: #313335; font-family: monospace; font-weight: bold}
.literal {color: #cc7832}
.ST4 {font-family: monospace; font-weight: bold; font-style: italic}
.ST0 {color: #287bde}
.string {color: #6a8759}
.number {color: #6897bb}
.ST3 {color: #9876aa}
.comment {color: #808080}
.whitespace {color: #505050}
.ST1 {color: #ffc66d; font-family: monospace; font-weight: bold; font-style: italic}
.ST2 {color: #9876aa; font-family: monospace; font-weight: bold; font-style: italic}
-->
</style>
</head>
<body>
<table width="100%"><tr><td align="center">/home/caleb/ASDV-Java/Semester 2/Assignments/lab7_CalebFontenot/src/main/java/com/calebfontenot/lab7_calebfontenot/occurrencesOfEachCharacter.java</td></tr></table>
<pre>
<span class="comment">/*</span>
<span class="comment"> * Click </span><span class="ST0">nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt</span><span class="comment"> to change this license</span>
<span class="comment"> * Click </span><span class="ST0">nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java</span><span class="comment"> to edit this template</span>
<span class="comment"> */</span>
<span class="literal">package</span> com.calebfontenot.lab7_calebfontenot;
<span class="literal">import</span> java.io.File;
<span class="literal">import</span> java.io.FileNotFoundException;
<span class="literal">import</span> java.util.Scanner;
<span class="comment">/**</span>
<span class="comment"> *</span>
<span class="comment"> * </span><span class="comment">@author</span> <span class="comment">caleb</span>
<span class="comment">*/</span>
<span class="literal">public</span> <span class="literal">class</span> occurrencesOfEachCharacter {
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">void</span> <span class="ST1">main</span>(String[] args) {
System.<span class="ST2">out</span>.println((<span class="literal">int</span>) <span class="string">&#39;</span><span class="string">a</span><span class="string">&#39;</span> + <span class="string">&quot;</span><span class="string">, </span><span class="string">&quot;</span> + (<span class="literal">int</span>) <span class="string">&#39;</span><span class="string">z</span><span class="string">&#39;</span>);
<span class="comment">// Number array for each letter in alphabet</span>
<span class="literal">int</span>[] letterCount = <span class="literal">new</span> <span class="literal">int</span>[<span class="number">26</span>];
Scanner input = <span class="literal">new</span> Scanner(System.<span class="ST2">in</span>);
System.<span class="ST2">out</span>.print(<span class="string">&quot;</span><span class="string">Enter a path to a file to scan: </span><span class="string">&quot;</span>);
File filePath = <span class="literal">new</span> File(input.nextLine());
<span class="literal">try</span> (Scanner fileScanner = <span class="literal">new</span> Scanner(filePath);) {
<span class="literal">char</span> currentChar;
<span class="literal">int</span> arrayIndex = <span class="number">0</span>;
<span class="comment">//Instruct scanner to delimit at everything</span>
fileScanner.useDelimiter(<span class="string">&quot;</span><span class="string">&quot;</span>);
<span class="literal">while</span> (fileScanner.hasNext()) {
currentChar = fileScanner.next().toLowerCase().charAt(<span class="number">0</span>);
arrayIndex = ((<span class="literal">int</span>) currentChar - <span class="number">97</span>); <span class="comment">//This will determine where in the array to increment at</span>
<span class="comment">//System.out.println(currentChar);</span>
<span class="literal">if</span> (currentChar &gt; <span class="string">&#39;</span><span class="string">a</span><span class="string">&#39;</span> &amp; currentChar &lt; <span class="string">&#39;</span><span class="string">z</span><span class="string">&#39;</span>) {
letterCount[arrayIndex]++;
}
}
} <span class="literal">catch</span> (FileNotFoundException ex) {
System.<span class="ST2">out</span>.println(ex);
}
<span class="comment">//Alright. We should have an array with a count of every char in the file.</span>
<span class="literal">for</span> (<span class="literal">int</span> i = <span class="number">0</span>; i &lt; letterCount.<span class="ST3">length</span>; ++i) {
<span class="literal">if</span> (letterCount[i] &gt; <span class="number">0</span>) {
System.<span class="ST2">out</span>.println(<span class="string">&quot;</span><span class="string">Number of </span><span class="string">&quot;</span> + Character.<span class="ST4">toUpperCase</span>((<span class="literal">char</span>) (i + <span class="number">97</span>)) + <span class="string">&quot;</span><span class="string">&#39;s: </span><span class="string">&quot;</span> + letterCount[i]);
}
}
}
}
</pre></body>
</html>