Finish lab 6
This commit is contained in:
parent
50674e0cef
commit
fb70cf62b6
@ -0,0 +1,74 @@
|
||||
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
|
||||
<html>
|
||||
<head>
|
||||
<title>CircleWithCheckedException.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/CircleWithCheckedException.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="comment">/**</span>
|
||||
<span class="comment"> *</span>
|
||||
<span class="comment"> * </span><span class="ST0">@author</span> <span class="comment">calebfontenot</span>
|
||||
<span class="comment">*/</span>
|
||||
<span class="literal">public</span> <span class="literal">class</span> CircleWithCheckedException {
|
||||
<span class="literal">private</span> <span class="literal">double</span> radius;
|
||||
|
||||
<span class="literal">public</span> CircleWithCheckedException() <span class="literal">throws</span> Exception
|
||||
{
|
||||
<span class="literal">this</span>(<span class="number">1.0</span>);
|
||||
}
|
||||
<span class="literal">public</span> CircleWithCheckedException(<span class="literal">double</span> radius) <span class="literal">throws</span> Exception
|
||||
{
|
||||
<span class="literal">if</span> (radius < <span class="number">0</span>) {
|
||||
<span class="literal">throw</span> <span class="literal">new</span> Exception(<span class="string">"</span><span class="string">Radius cannot be negative.</span><span class="string">"</span>);
|
||||
}
|
||||
<span class="literal">this</span>.radius = radius;
|
||||
}
|
||||
|
||||
<span class="literal">public</span> <span class="literal">double</span> getRadius() {
|
||||
<span class="literal">return</span> radius;
|
||||
}
|
||||
<span class="literal">public</span> <span class="literal">void</span> setRadius(<span class="literal">double</span> radius) <span class="literal">throws</span> Exception {
|
||||
<span class="literal">if</span> (radius < <span class="number">0</span>) {
|
||||
<span class="literal">throw</span> <span class="literal">new</span> Exception(<span class="string">"</span><span class="string">Radius cannot be negative.</span><span class="string">"</span>);
|
||||
}
|
||||
<span class="literal">this</span>.radius = radius;
|
||||
}
|
||||
|
||||
@Override
|
||||
<span class="literal">public</span> String toString() {
|
||||
<span class="literal">return</span> <span class="string">"</span><span class="string">CircleWithCheckedException{</span><span class="string">"</span> + <span class="string">"</span><span class="string">radius=</span><span class="string">"</span> + radius + <span class="string">'</span><span class="string">}</span><span class="string">'</span>;
|
||||
}
|
||||
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">void</span> main(String[] args) {
|
||||
<span class="literal">try</span> {
|
||||
System.out.println(<span class="literal">new</span> CircleWithCheckedException(<span class="number">5</span>));
|
||||
System.out.println(<span class="literal">new</span> CircleWithCheckedException(-<span class="number">5</span>));
|
||||
System.out.println(<span class="literal">new</span> CircleWithCheckedException(<span class="number">10</span>));
|
||||
} <span class="literal">catch</span> (Exception ex) {
|
||||
System.err.println(<span class="string">"</span><span class="string">an exception occured: </span><span class="string">"</span> + ex.getMessage());
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
</pre></body>
|
||||
</html>
|
@ -0,0 +1,40 @@
|
||||
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
|
||||
<html>
|
||||
<head>
|
||||
<title>Lab6Exceptions_CalebFontenot.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}
|
||||
.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/Lab6Exceptions_CalebFontenot.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"> */</span>
|
||||
|
||||
<span class="literal">package</span> com.mycompany.lab6.exceptions_calebfontenot;
|
||||
|
||||
<span class="comment">/**</span>
|
||||
<span class="comment"> *</span>
|
||||
<span class="comment"> * </span><span class="ST0">@author</span> <span class="comment">calebfontenot</span>
|
||||
<span class="comment">*/</span>
|
||||
<span class="literal">public</span> <span class="literal">class</span> Lab6Exceptions_CalebFontenot {
|
||||
|
||||
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">void</span> main(String[] args) {
|
||||
System.out.println(<span class="string">"</span><span class="string">Hello World!</span><span class="string">"</span>);
|
||||
}
|
||||
}
|
||||
|
||||
</pre></body>
|
||||
</html>
|
@ -0,0 +1,73 @@
|
||||
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
|
||||
<html>
|
||||
<head>
|
||||
<title>ProcessScoresInTextFile.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}
|
||||
.string {color: #6a8759}
|
||||
.number {color: #6897bb}
|
||||
.comment {color: #808080}
|
||||
.whitespace {color: #505050}
|
||||
.ST2 {color: #ffc66d; font-family: monospace; font-style: italic}
|
||||
.ST3 {color: #9876aa; font-family: monospace; font-style: italic}
|
||||
.ST1 {color: #808080; font-family: monospace; font-weight: bold}
|
||||
.ST0 {color: #287bde}
|
||||
.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/ProcessScoresInTextFile.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.mycompany.lab6.exceptions_calebfontenot;
|
||||
|
||||
<span class="literal">import</span> java.io.File;
|
||||
<span class="literal">import</span> java.util.ArrayList;
|
||||
<span class="literal">import</span> java.util.Scanner;
|
||||
|
||||
<span class="comment">/**</span>
|
||||
<span class="comment"> *</span>
|
||||
<span class="comment"> * </span><span class="ST1">@author</span> <span class="comment">caleb</span>
|
||||
<span class="comment">*/</span>
|
||||
<span class="literal">public</span> <span class="literal">class</span> ProcessScoresInTextFile {
|
||||
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">void</span> <span class="ST2">main</span>(String[] args) <span class="literal">throws</span> Exception
|
||||
{
|
||||
File file = <span class="literal">new</span> File(<span class="string">"</span><span class="string">scores.txt</span><span class="string">"</span>);
|
||||
|
||||
<span class="comment">// Create a scanner for the file</span>
|
||||
Scanner input = <span class="literal">new</span> Scanner(file);
|
||||
|
||||
ArrayList<String> nameAR = <span class="literal">new</span> ArrayList<>();
|
||||
ArrayList<Integer> intAR = <span class="literal">new</span> ArrayList<>();
|
||||
|
||||
<span class="comment">// Read data from file</span>
|
||||
<span class="literal">int</span> iterate = <span class="number">0</span>;
|
||||
<span class="literal">int</span> score;
|
||||
<span class="literal">while</span> (input.hasNext()) {
|
||||
iterate++;
|
||||
String name = input.next();
|
||||
score = input.nextInt();
|
||||
nameAR.add(name);
|
||||
intAR.add((Integer) score);
|
||||
}
|
||||
<span class="literal">double</span> average = <span class="number">0</span>;
|
||||
<span class="literal">for</span> (<span class="literal">int</span> i = <span class="number">0</span>; i < intAR.size(); ++i) {
|
||||
System.<span class="ST3">out</span>.print(nameAR.get(i) + <span class="string">"</span> <span class="string">"</span>);
|
||||
System.<span class="ST3">out</span>.println(intAR.get(i));
|
||||
average += intAR.get(i);
|
||||
}
|
||||
average = (average / intAR.size());
|
||||
System.<span class="ST3">out</span>.println(<span class="string">"</span><span class="string">Average: </span><span class="string">"</span> + average);
|
||||
}
|
||||
}
|
||||
|
||||
</pre></body>
|
||||
</html>
|
@ -0,0 +1,73 @@
|
||||
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
|
||||
<html>
|
||||
<head>
|
||||
<title>ReadFileFromURL.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}
|
||||
.string {color: #6a8759}
|
||||
.number {color: #6897bb}
|
||||
.comment {color: #808080}
|
||||
.whitespace {color: #505050}
|
||||
.ST2 {color: #ffc66d; font-family: monospace; font-style: italic}
|
||||
.ST3 {color: #9876aa; font-family: monospace; font-style: italic}
|
||||
.ST1 {color: #808080; font-family: monospace; font-weight: bold}
|
||||
.ST0 {color: #287bde}
|
||||
.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/ReadFileFromURL.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.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="ST1">@author</span> <span class="comment">caleb</span>
|
||||
<span class="comment">*/</span>
|
||||
<span class="literal">public</span> <span class="literal">class</span> ReadFileFromURL {
|
||||
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">void</span> <span class="ST2">main</span>(String[] args)
|
||||
{
|
||||
<span class="literal">boolean</span> continueLoop = <span class="literal">false</span>;
|
||||
<span class="literal">boolean</span> exceptionThrown = <span class="literal">false</span>;
|
||||
<span class="literal">do</span> {
|
||||
System.<span class="ST3">out</span>.print(<span class="string">"</span><span class="string">Enter a URL: </span><span class="string">"</span>);
|
||||
String URLString = <span class="literal">new</span> Scanner(System.<span class="ST3">in</span>).next();
|
||||
|
||||
<span class="literal">try</span> {
|
||||
java.net.URL url = <span class="literal">new</span> java.net.URL(URLString);
|
||||
<span class="literal">int</span> count = <span class="number">0</span>;
|
||||
Scanner input = <span class="literal">new</span> Scanner(url.openStream());
|
||||
<span class="literal">while</span> (input.hasNext()) {
|
||||
String line = input.nextLine();
|
||||
count += line.length();
|
||||
}
|
||||
System.<span class="ST3">out</span>.println(<span class="string">"</span><span class="string">The file size is </span><span class="string">"</span> + count + <span class="string">"</span><span class="string"> characters</span><span class="string">"</span>);
|
||||
exceptionThrown = <span class="literal">false</span>;
|
||||
} <span class="literal">catch</span> (java.net.MalformedURLException ex) {
|
||||
System.<span class="ST3">out</span>.println(<span class="string">"</span><span class="string">Invalid URL</span><span class="string">"</span>);
|
||||
exceptionThrown = <span class="literal">true</span>;
|
||||
} <span class="literal">catch</span> (java.io.IOException ex) {
|
||||
System.<span class="ST3">out</span>.println(<span class="string">"</span><span class="string">IO Errors</span><span class="string">"</span>);
|
||||
}
|
||||
<span class="literal">if</span> (exceptionThrown) {
|
||||
continueLoop = <span class="literal">true</span>;
|
||||
} <span class="literal">else</span> {
|
||||
continueLoop = <span class="literal">false</span>;
|
||||
}
|
||||
} <span class="literal">while</span> (continueLoop);
|
||||
}
|
||||
}
|
||||
|
||||
</pre></body>
|
||||
</html>
|
@ -0,0 +1,61 @@
|
||||
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
|
||||
<html>
|
||||
<head>
|
||||
<title>ReadFileFromWeb.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}
|
||||
.string {color: #6a8759}
|
||||
.number {color: #6897bb}
|
||||
.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/ReadFileFromWeb.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> ReadFileFromWeb {
|
||||
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">void</span> main(String[] args) {
|
||||
System.out.print(<span class="string">"</span><span class="string">Enter a URL: </span><span class="string">"</span>);
|
||||
String URLString = <span class="literal">new</span> Scanner(System.in).next();
|
||||
<span class="literal">try</span> {
|
||||
java.net.URL url = <span class="literal">new</span> java.net.URL(URLString);
|
||||
<span class="literal">int</span> count = <span class="number">0</span>;
|
||||
String text =<span class="string">""</span>;
|
||||
Scanner input = <span class="literal">new</span> Scanner(url.openStream());
|
||||
<span class="literal">while</span> (input.hasNext()) {
|
||||
String line = input.nextLine();
|
||||
count += line.length();
|
||||
text += line;
|
||||
}
|
||||
System.out.println(<span class="string">"</span><span class="string">The file size is </span><span class="string">"</span> + count + <span class="string">"</span><span class="string"> characters.</span><span class="string">"</span>);
|
||||
System.out.println(text);
|
||||
} <span class="literal">catch</span> (java.net.MalformedURLException ex) {
|
||||
System.out.println(<span class="string">"</span><span class="string">Invalid URL</span><span class="string">"</span>);
|
||||
}
|
||||
<span class="literal">catch</span> (java.io.IOException ex) {
|
||||
System.out.println(<span class="string">"</span><span class="string">IO Errors!</span><span class="string">"</span>);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
</pre></body>
|
||||
</html>
|
@ -0,0 +1,80 @@
|
||||
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
|
||||
<html>
|
||||
<head>
|
||||
<title>ReadFileFromWeb1.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}
|
||||
.string {color: #6a8759}
|
||||
.number {color: #6897bb}
|
||||
.comment {color: #808080}
|
||||
.whitespace {color: #505050}
|
||||
.ST2 {color: #ffc66d; font-family: monospace; font-style: italic}
|
||||
.ST3 {color: #9876aa; font-family: monospace; font-style: italic}
|
||||
.ST1 {color: #808080; font-family: monospace; font-weight: bold}
|
||||
.ST0 {color: #287bde}
|
||||
.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/ReadFileFromWeb1.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.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="ST1">@author</span> <span class="comment">caleb</span>
|
||||
<span class="comment">*/</span>
|
||||
<span class="literal">public</span> <span class="literal">class</span> ReadFileFromWeb1 {
|
||||
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">void</span> <span class="ST2">main</span>(String[] args) {
|
||||
System.<span class="ST3">out</span>.print(<span class="string">"</span><span class="string">Enter a URL (defaults to </span><span class="literal">\"</span><span class="ST0">https://calebfontenot.com</span><span class="ST0">\</span><span class="literal">"</span><span class="string">): </span><span class="string">"</span>);
|
||||
String URLinput = <span class="literal">new</span> Scanner(System.<span class="ST3">in</span>).nextLine();
|
||||
String URLString;
|
||||
<span class="literal">if</span> (URLinput.isEmpty()) {
|
||||
URLString = <span class="string">"</span><span class="ST0">https://calebfontenot.com</span><span class="string">"</span>;
|
||||
}
|
||||
<span class="literal">else</span> {
|
||||
URLString = URLinput;
|
||||
}
|
||||
<span class="literal">try</span> {
|
||||
java.net.URL url = <span class="literal">new</span> java.net.URL(URLString);
|
||||
<span class="literal">int</span> count = <span class="number">0</span>, divCount = <span class="number">0</span>, pCount = <span class="number">0</span>;
|
||||
String text =<span class="string">""</span>;
|
||||
Scanner input = <span class="literal">new</span> Scanner(url.openStream());
|
||||
<span class="literal">while</span> (input.hasNext()) {
|
||||
String line = input.nextLine();
|
||||
count += line.length();
|
||||
text += line;
|
||||
<span class="literal">if</span> (line.contains(<span class="string">"</span><span class="string"><div</span><span class="string">"</span>)) {
|
||||
divCount++;
|
||||
}
|
||||
<span class="literal">if</span> (line.contains(<span class="string">"</span><span class="string"><p</span><span class="string">"</span>)) {
|
||||
pCount++;
|
||||
}
|
||||
|
||||
}
|
||||
System.<span class="ST3">out</span>.println(<span class="string">"</span><span class="string">The file size is </span><span class="string">"</span> + count + <span class="string">"</span><span class="string"> characters.</span><span class="string">"</span>);
|
||||
System.<span class="ST3">out</span>.println(<span class="string">"</span><span class="string">There are </span><span class="string">"</span> + divCount + <span class="string">"</span><span class="string"> <div> tags on this site.</span><span class="string">"</span>);
|
||||
System.<span class="ST3">out</span>.println(<span class="string">"</span><span class="string">There are </span><span class="string">"</span> + pCount + <span class="string">"</span><span class="string"> <p> tags on this site.</span><span class="string">"</span>);
|
||||
System.<span class="ST3">out</span>.println(text);
|
||||
} <span class="literal">catch</span> (java.net.MalformedURLException ex) {
|
||||
System.<span class="ST3">out</span>.println(<span class="string">"</span><span class="string">Invalid URL</span><span class="string">"</span>);
|
||||
}
|
||||
<span class="literal">catch</span> (java.io.IOException ex) {
|
||||
System.<span class="ST3">out</span>.println(<span class="string">"</span><span class="string">IO Errors!</span><span class="string">"</span>);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
</pre></body>
|
||||
</html>
|
@ -0,0 +1,57 @@
|
||||
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
|
||||
<html>
|
||||
<head>
|
||||
<title>TestFileClass.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}
|
||||
.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/TestFileClass.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> TestFileClass {
|
||||
<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">"</span><span class="string">Enter a file path or directory: </span><span class="string">"</span>);
|
||||
String filePath = input.nextLine();
|
||||
testFile(filePath);
|
||||
}
|
||||
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">void</span> testFile(String filePath) {
|
||||
java.io.File file = <span class="literal">new</span> java.io.File(filePath);
|
||||
System.out.println(<span class="string">"</span><span class="string">Does it exist? </span><span class="string">"</span> + file.exists());
|
||||
System.out.println(<span class="string">"</span><span class="string">The file has </span><span class="string">"</span> + file.length() + <span class="string">"</span><span class="string"> bytes.</span><span class="string">"</span>);
|
||||
System.out.println(<span class="string">"</span><span class="string">Can it be read? </span><span class="string">"</span> + file.canRead());
|
||||
System.out.println(<span class="string">"</span><span class="string">Can it be written? </span><span class="string">"</span> + file.canWrite());
|
||||
System.out.println(<span class="string">"</span><span class="string">Is it a directory? </span><span class="string">"</span> + file.isDirectory());
|
||||
System.out.println(<span class="string">"</span><span class="string">Is it a file? </span><span class="string">"</span> + file.isFile());
|
||||
System.out.println(<span class="string">"</span><span class="string">Is it absolute? </span><span class="string">"</span> + file.isAbsolute());
|
||||
System.out.println(<span class="string">"</span><span class="string">Is it hidden? </span><span class="string">"</span> + file.isHidden());
|
||||
System.out.println(<span class="string">"</span><span class="string">The absolute path to this file is </span><span class="string">"</span> + file.getAbsolutePath());
|
||||
System.out.println(<span class="string">"</span><span class="string">Last modified on </span><span class="string">"</span> + <span class="literal">new</span> java.util.Date(file.lastModified()));
|
||||
}
|
||||
}
|
||||
|
||||
</pre></body>
|
||||
</html>
|
@ -0,0 +1,68 @@
|
||||
<!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 < 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 >= <span class="string">'</span><span class="string">A</span><span class="string">'</span> && ch <= <span class="string">'</span><span class="string">F</span><span class="string">'</span>) {
|
||||
<span class="literal">return</span> <span class="number">10</span> + ch - <span class="string">'</span><span class="string">A</span><span class="string">'</span>;
|
||||
} <span class="literal">else</span> <span class="literal">if</span> (ch >= <span class="string">'</span><span class="string">0</span><span class="string">'</span> && ch <= <span class="string">'</span><span class="string">9</span><span class="string">'</span>){ <span class="comment">// ch is '0', '1', or '9'</span>
|
||||
<span class="literal">return</span> ch - <span class="string">'</span><span class="string">0</span><span class="string">'</span>;
|
||||
} <span class="literal">else</span> {
|
||||
<span class="literal">throw</span> <span class="literal">new</span> NumberFormatException(<span class="string">"</span><span class="string">Invalid hex character</span><span class="string">"</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">"</span><span class="string">Enter a hex number or q/Q to quit: </span><span class="string">"</span>);
|
||||
String hex = input.nextLine();
|
||||
<span class="literal">while</span> (<span class="string">"</span><span class="string">q</span><span class="string">"</span>.equals(hex.toLowerCase()) == <span class="literal">false</span>) {
|
||||
System.out.println(<span class="string">"</span><span class="string">The decimal value for hex number </span><span class="string">"</span> + hex + <span class="string">"</span><span class="string"> is </span><span class="string">"</span> + hexToDecimal(hex.toUpperCase()));
|
||||
System.out.print(<span class="string">"</span><span class="string">Emter a hex number: </span><span class="string">"</span>);
|
||||
hex = input.nextLine();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
</pre></body>
|
||||
</html>
|
@ -0,0 +1,75 @@
|
||||
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
|
||||
<html>
|
||||
<head>
|
||||
<title>Triangle.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/Triangle.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> Triangle <span class="literal">implements</span> Geo{
|
||||
<span class="literal">private</span> <span class="literal">double</span> side1 = <span class="number">1.0</span>, side2 = <span class="number">1.0</span>, side3 = <span class="number">1.0</span>;
|
||||
|
||||
<span class="literal">public</span> Triangle(<span class="literal">double</span> side1, <span class="literal">double</span> side2, <span class="literal">double</span> side3) {
|
||||
<span class="literal">this</span>.side1 = side1;
|
||||
<span class="literal">this</span>.side2 = side2;
|
||||
<span class="literal">this</span>.side3 = side3;
|
||||
}
|
||||
|
||||
@Override
|
||||
<span class="literal">public</span> <span class="literal">double</span> getArea() {
|
||||
<span class="literal">double</span> s = (side1 + side2 + side3) / <span class="number">2</span>;
|
||||
<span class="literal">return</span> Math.sqrt(s * (s - side1) * (s - side2) * (s - side3));
|
||||
}
|
||||
@Override
|
||||
<span class="literal">public</span> <span class="literal">double</span> getPerimeter() {
|
||||
<span class="literal">return</span> side1 + side2 + side3;
|
||||
}
|
||||
@Override
|
||||
<span class="literal">public</span> String toString() {
|
||||
<span class="literal">return</span> <span class="string">"</span><span class="string">Triangle: side1 = </span><span class="string">"</span> + side1 + <span class="string">"</span><span class="string"> side2 = </span><span class="string">"</span> + side2 + <span class="string">"</span><span class="string"> side3 = </span><span class="string">"</span> + side3;
|
||||
}
|
||||
<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.println(<span class="string">"</span><span class="string">Enter the three sides: </span><span class="string">"</span>);
|
||||
<span class="literal">double</span> side1 = input.nextDouble();
|
||||
<span class="literal">double</span> side2 = input.nextDouble();
|
||||
<span class="literal">double</span> side3 = input.nextDouble();
|
||||
Triangle triangle = <span class="literal">new</span> Triangle(side1, side2, side3);
|
||||
System.out.println(<span class="string">"</span><span class="string">The area is </span><span class="string">"</span> + triangle.getArea());
|
||||
System.out.println(<span class="string">"</span><span class="string">The perimeter is </span><span class="string">"</span> + triangle.getPerimeter());
|
||||
System.out.println(triangle);
|
||||
}
|
||||
}
|
||||
<span class="literal">interface</span> Geo {
|
||||
<span class="literal">double</span> getArea();
|
||||
<span class="literal">public</span> <span class="literal">double</span> getPerimeter();
|
||||
}
|
||||
|
||||
</pre></body>
|
||||
</html>
|
@ -0,0 +1,66 @@
|
||||
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
|
||||
<html>
|
||||
<head>
|
||||
<title>WriteReadData.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}
|
||||
.string {color: #6a8759}
|
||||
.number {color: #6897bb}
|
||||
.comment {color: #808080}
|
||||
.whitespace {color: #505050}
|
||||
.ST2 {color: #ffc66d; font-family: monospace; font-style: italic}
|
||||
.ST3 {color: #9876aa; font-family: monospace; font-style: italic}
|
||||
.ST1 {color: #808080; font-family: monospace; font-weight: bold}
|
||||
.ST0 {color: #287bde}
|
||||
.literal {color: #cc7832}
|
||||
.ST4 {font-family: monospace; font-style: italic}
|
||||
-->
|
||||
</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/WriteReadData.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.mycompany.lab6.exceptions_calebfontenot;
|
||||
|
||||
<span class="literal">import</span> java.io.File;
|
||||
<span class="literal">import</span> java.io.PrintWriter;
|
||||
<span class="literal">import</span> java.util.Scanner;
|
||||
|
||||
<span class="comment">/**</span>
|
||||
<span class="comment"> *</span>
|
||||
<span class="comment"> * </span><span class="ST1">@author</span> <span class="comment">caleb</span>
|
||||
<span class="comment">*/</span>
|
||||
<span class="literal">public</span> <span class="literal">class</span> WriteReadData {
|
||||
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">void</span> <span class="ST2">main</span>(String[] args) <span class="literal">throws</span> Exception
|
||||
{
|
||||
File file = <span class="literal">new</span> File(<span class="string">"</span><span class="string">rw.txt</span><span class="string">"</span>);
|
||||
<span class="literal">int</span> randInt;
|
||||
<span class="literal">if</span> (!file.exists()) {
|
||||
System.<span class="ST3">out</span>.println(<span class="string">"</span><span class="string">File does not exist! Creating it...</span><span class="string">"</span>);
|
||||
<span class="literal">try</span> (PrintWriter output = <span class="literal">new</span> PrintWriter(file);) {
|
||||
<span class="literal">for</span> (<span class="literal">int</span> i = <span class="number">0</span>; i < <span class="number">100</span>; ++i) {
|
||||
randInt = (<span class="literal">int</span>) (Math.<span class="ST4">random</span>() * <span class="number">100</span>);
|
||||
System.<span class="ST3">out</span>.println(<span class="string">"</span><span class="string">Writing </span><span class="string">"</span> + randInt + <span class="string">"</span><span class="string"> to file...</span><span class="string">"</span>);
|
||||
output.write(Integer.<span class="ST4">toString</span>(randInt) + <span class="string">"</span><span class="literal">\n</span><span class="string">"</span>);
|
||||
}
|
||||
} <span class="literal">catch</span> (Exception ex) {
|
||||
System.<span class="ST3">out</span>.println(ex);
|
||||
}
|
||||
}
|
||||
Scanner input = <span class="literal">new</span> Scanner(file);
|
||||
System.<span class="ST3">out</span>.println(<span class="string">"</span><span class="string">Reading file contents:</span><span class="string">"</span>);
|
||||
<span class="literal">while</span> (input.hasNext()) {
|
||||
System.<span class="ST3">out</span>.println(input.next());
|
||||
}
|
||||
}
|
||||
}
|
||||
</pre></body>
|
||||
</html>
|
@ -0,0 +1,61 @@
|
||||
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
|
||||
<html>
|
||||
<head>
|
||||
<title>WriteWithAutoClose.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}
|
||||
.string {color: #6a8759}
|
||||
.number {color: #6897bb}
|
||||
.comment {color: #808080}
|
||||
.whitespace {color: #505050}
|
||||
.ST2 {color: #ffc66d; font-family: monospace; font-style: italic}
|
||||
.ST3 {color: #9876aa; font-family: monospace; font-style: italic}
|
||||
.ST1 {color: #808080; font-family: monospace; font-weight: bold}
|
||||
.ST0 {color: #287bde}
|
||||
.literal {color: #cc7832}
|
||||
.ST4 {font-family: monospace; font-style: italic}
|
||||
-->
|
||||
</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/WriteWithAutoClose.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.mycompany.lab6.exceptions_calebfontenot;
|
||||
|
||||
<span class="literal">import</span> java.io.IOException;
|
||||
|
||||
<span class="comment">/**</span>
|
||||
<span class="comment"> *</span>
|
||||
<span class="comment"> * </span><span class="ST1">@author</span> <span class="comment">caleb</span>
|
||||
<span class="comment">*/</span>
|
||||
<span class="literal">public</span> <span class="literal">class</span> WriteWithAutoClose {
|
||||
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">void</span> <span class="ST2">main</span>(String[] args) <span class="literal">throws</span> IOException
|
||||
{
|
||||
java.io.File file = <span class="literal">new</span> java.io.File(<span class="string">"</span><span class="string">scores.txt</span><span class="string">"</span>);
|
||||
<span class="literal">if</span> (file.exists()) {
|
||||
System.<span class="ST3">out</span>.println(<span class="string">"</span><span class="string">File already exists!</span><span class="string">"</span>);
|
||||
System.<span class="ST4">exit</span>(<span class="number">0</span>);
|
||||
}
|
||||
|
||||
<span class="comment">//throw new IOException(); //Just for testing :)</span>
|
||||
|
||||
<span class="literal">try</span> (java.io.PrintWriter output = <span class="literal">new</span> java.io.PrintWriter(file);) {
|
||||
<span class="comment">// Write formatted output to the file</span>
|
||||
output.print(<span class="string">"</span><span class="string">John T Smith </span><span class="string">"</span>);
|
||||
output.println(<span class="number">9</span><span class="number">0</span>);
|
||||
output.print(<span class="string">"</span><span class="string">Eric K Jones </span><span class="string">"</span>);
|
||||
output.println(<span class="number">8</span><span class="number">5</span>);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
</pre></body>
|
||||
</html>
|
@ -0,0 +1,63 @@
|
||||
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
|
||||
<html>
|
||||
<head>
|
||||
<title>WriteWithAutoClose2.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}
|
||||
.string {color: #6a8759}
|
||||
.number {color: #6897bb}
|
||||
.comment {color: #808080}
|
||||
.whitespace {color: #505050}
|
||||
.ST2 {color: #ffc66d; font-family: monospace; font-style: italic}
|
||||
.ST3 {color: #9876aa; font-family: monospace; font-style: italic}
|
||||
.ST1 {color: #808080; font-family: monospace; font-weight: bold}
|
||||
.ST0 {color: #287bde}
|
||||
.literal {color: #cc7832}
|
||||
.ST4 {font-family: monospace; font-style: italic}
|
||||
-->
|
||||
</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/WriteWithAutoClose2.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.mycompany.lab6.exceptions_calebfontenot;
|
||||
|
||||
<span class="comment">import</span> <span class="comment">java</span><span class="comment">.</span><span class="comment">io</span><span class="comment">.</span><span class="comment">IOException</span><span class="comment">;</span>
|
||||
|
||||
<span class="comment">/**</span>
|
||||
<span class="comment"> *</span>
|
||||
<span class="comment"> * </span><span class="ST1">@author</span> <span class="comment">caleb</span>
|
||||
<span class="comment">*/</span>
|
||||
<span class="literal">public</span> <span class="literal">class</span> WriteWithAutoClose2 {
|
||||
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">void</span> <span class="ST2">main</span>(String[] args) {
|
||||
<span class="literal">try</span> {
|
||||
java.io.File file = <span class="literal">new</span> java.io.File(<span class="string">"</span><span class="string">scores.txt</span><span class="string">"</span>);
|
||||
<span class="literal">if</span> (file.exists()) {
|
||||
System.<span class="ST3">out</span>.println(<span class="string">"</span><span class="string">File already exists!</span><span class="string">"</span>);
|
||||
System.<span class="ST4">exit</span>(<span class="number">0</span>);
|
||||
}
|
||||
<span class="comment">//throw new IOException(); //Just for testing :)</span>
|
||||
|
||||
<span class="literal">try</span> (java.io.PrintWriter output = <span class="literal">new</span> java.io.PrintWriter(file);) {
|
||||
<span class="comment">// Write formatted output to the file</span>
|
||||
output.print(<span class="string">"</span><span class="string">John T Smith </span><span class="string">"</span>);
|
||||
output.println(<span class="number">9</span><span class="number">0</span>);
|
||||
output.print(<span class="string">"</span><span class="string">Eric K Jones </span><span class="string">"</span>);
|
||||
output.println(<span class="number">8</span><span class="number">5</span>);
|
||||
}
|
||||
} <span class="literal">catch</span> (Exception ex) {
|
||||
System.<span class="ST3">err</span>.println(<span class="string">"</span><span class="string">Cought exception: </span><span class="string">"</span> + ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
</pre></body>
|
||||
</html>
|
100
Semester 2/Assignments/lab6-Exceptions_CalebFontenot/rw.txt
Normal file
100
Semester 2/Assignments/lab6-Exceptions_CalebFontenot/rw.txt
Normal file
@ -0,0 +1,100 @@
|
||||
66
|
||||
31
|
||||
93
|
||||
98
|
||||
45
|
||||
64
|
||||
73
|
||||
43
|
||||
97
|
||||
21
|
||||
98
|
||||
20
|
||||
18
|
||||
87
|
||||
91
|
||||
43
|
||||
30
|
||||
71
|
||||
15
|
||||
14
|
||||
53
|
||||
4
|
||||
81
|
||||
18
|
||||
76
|
||||
22
|
||||
88
|
||||
89
|
||||
76
|
||||
51
|
||||
94
|
||||
56
|
||||
64
|
||||
72
|
||||
96
|
||||
87
|
||||
43
|
||||
66
|
||||
61
|
||||
46
|
||||
46
|
||||
44
|
||||
32
|
||||
26
|
||||
90
|
||||
97
|
||||
97
|
||||
75
|
||||
97
|
||||
11
|
||||
95
|
||||
55
|
||||
9
|
||||
95
|
||||
17
|
||||
52
|
||||
19
|
||||
64
|
||||
31
|
||||
66
|
||||
62
|
||||
15
|
||||
77
|
||||
28
|
||||
73
|
||||
55
|
||||
34
|
||||
51
|
||||
72
|
||||
51
|
||||
88
|
||||
14
|
||||
98
|
||||
45
|
||||
0
|
||||
97
|
||||
66
|
||||
78
|
||||
94
|
||||
95
|
||||
5
|
||||
54
|
||||
92
|
||||
63
|
||||
69
|
||||
72
|
||||
91
|
||||
94
|
||||
43
|
||||
79
|
||||
56
|
||||
75
|
||||
54
|
||||
26
|
||||
37
|
||||
49
|
||||
77
|
||||
40
|
||||
6
|
||||
28
|
@ -1,2 +1,4 @@
|
||||
John T Smith 90
|
||||
Eric K Jones 85
|
||||
Caleb 90
|
||||
Hailey 85
|
||||
Davin 100
|
||||
Tyler 95
|
||||
|
@ -0,0 +1,45 @@
|
||||
/*
|
||||
* 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.io.File;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Scanner;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author caleb
|
||||
*/
|
||||
public class ProcessScoresInTextFile {
|
||||
public static void main(String[] args) throws Exception
|
||||
{
|
||||
File file = new File("scores.txt");
|
||||
|
||||
// Create a scanner for the file
|
||||
Scanner input = new Scanner(file);
|
||||
|
||||
ArrayList<String> nameAR = new ArrayList<>();
|
||||
ArrayList<Integer> intAR = new ArrayList<>();
|
||||
|
||||
// Read data from file
|
||||
int iterate = 0;
|
||||
int score;
|
||||
while (input.hasNext()) {
|
||||
iterate++;
|
||||
String name = input.next();
|
||||
score = input.nextInt();
|
||||
nameAR.add(name);
|
||||
intAR.add((Integer) score);
|
||||
}
|
||||
double average = 0;
|
||||
for (int i = 0; i < intAR.size(); ++i) {
|
||||
System.out.print(nameAR.get(i) + " ");
|
||||
System.out.println(intAR.get(i));
|
||||
average += intAR.get(i);
|
||||
}
|
||||
average = (average / intAR.size());
|
||||
System.out.println("Average: " + average);
|
||||
}
|
||||
}
|
@ -0,0 +1,45 @@
|
||||
/*
|
||||
* 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 ReadFileFromURL {
|
||||
public static void main(String[] args)
|
||||
{
|
||||
boolean continueLoop = false;
|
||||
boolean exceptionThrown = false;
|
||||
do {
|
||||
System.out.print("Enter a URL: ");
|
||||
String URLString = new Scanner(System.in).next();
|
||||
|
||||
try {
|
||||
java.net.URL url = new java.net.URL(URLString);
|
||||
int count = 0;
|
||||
Scanner input = new Scanner(url.openStream());
|
||||
while (input.hasNext()) {
|
||||
String line = input.nextLine();
|
||||
count += line.length();
|
||||
}
|
||||
System.out.println("The file size is " + count + " characters");
|
||||
exceptionThrown = false;
|
||||
} catch (java.net.MalformedURLException ex) {
|
||||
System.out.println("Invalid URL");
|
||||
exceptionThrown = true;
|
||||
} catch (java.io.IOException ex) {
|
||||
System.out.println("IO Errors");
|
||||
}
|
||||
if (exceptionThrown) {
|
||||
continueLoop = true;
|
||||
} else {
|
||||
continueLoop = false;
|
||||
}
|
||||
} while (continueLoop);
|
||||
}
|
||||
}
|
@ -0,0 +1,52 @@
|
||||
/*
|
||||
* 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 ReadFileFromWeb1 {
|
||||
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;
|
||||
}
|
||||
try {
|
||||
java.net.URL url = new java.net.URL(URLString);
|
||||
int count = 0, divCount = 0, pCount = 0;
|
||||
String text ="";
|
||||
Scanner input = new Scanner(url.openStream());
|
||||
while (input.hasNext()) {
|
||||
String line = input.nextLine();
|
||||
count += line.length();
|
||||
text += line;
|
||||
if (line.contains("<div")) {
|
||||
divCount++;
|
||||
}
|
||||
if (line.contains("<p")) {
|
||||
pCount++;
|
||||
}
|
||||
|
||||
}
|
||||
System.out.println("The file size is " + count + " characters.");
|
||||
System.out.println("There are " + divCount + " <div> tags on this site.");
|
||||
System.out.println("There are " + pCount + " <p> tags on this site.");
|
||||
System.out.println(text);
|
||||
} catch (java.net.MalformedURLException ex) {
|
||||
System.out.println("Invalid URL");
|
||||
}
|
||||
catch (java.io.IOException ex) {
|
||||
System.out.println("IO Errors!");
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,38 @@
|
||||
/*
|
||||
* 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.io.File;
|
||||
import java.io.PrintWriter;
|
||||
import java.util.Scanner;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author caleb
|
||||
*/
|
||||
public class WriteReadData {
|
||||
public static void main(String[] args) throws Exception
|
||||
{
|
||||
File file = new File("rw.txt");
|
||||
int randInt;
|
||||
if (!file.exists()) {
|
||||
System.out.println("File does not exist! Creating it...");
|
||||
try (PrintWriter output = new PrintWriter(file);) {
|
||||
for (int i = 0; i < 100; ++i) {
|
||||
randInt = (int) (Math.random() * 100);
|
||||
System.out.println("Writing " + randInt + " to file...");
|
||||
output.write(Integer.toString(randInt) + "\n");
|
||||
}
|
||||
} catch (Exception ex) {
|
||||
System.out.println(ex);
|
||||
}
|
||||
}
|
||||
Scanner input = new Scanner(file);
|
||||
System.out.println("Reading file contents:");
|
||||
while (input.hasNext()) {
|
||||
System.out.println(input.next());
|
||||
}
|
||||
}
|
||||
}
|
BIN
Semester 2/ZIPs/lab6-Exceptions_CalebFontenot.zip
Normal file
BIN
Semester 2/ZIPs/lab6-Exceptions_CalebFontenot.zip
Normal file
Binary file not shown.
Loading…
x
Reference in New Issue
Block a user