website/static/~brozek/index.html?labaide%2Ffall2017%2Fcpsc220%2Fnov6.html
2022-02-15 01:14:58 -05:00

182 lines
6.1 KiB
HTML

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="author" content="Brandon Rozek">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="robots" content="noindex" />
<title>Brandon Rozek</title>
<link rel="stylesheet" href="themes/bitsandpieces/styles/main.css" type="text/css" />
<link rel="stylesheet" href="themes/bitsandpieces/styles/highlightjs-github.css" type="text/css" />
</head>
<body>
<aside class="main-nav">
<nav>
<ul>
<li class="menuitem ">
<a href="index.html%3Findex.html" data-shortcut="">
Home
</a>
</li>
<li class="menuitem ">
<a href="index.html%3Fcourses.html" data-shortcut="">
Courses
</a>
</li>
<li class="menuitem ">
<a href="index.html%3Flabaide.html" data-shortcut="">
Lab Aide
</a>
</li>
<li class="menuitem ">
<a href="index.html%3Fpresentations.html" data-shortcut="">
Presentations
</a>
</li>
<li class="menuitem ">
<a href="index.html%3Fresearch.html" data-shortcut="">
Research
</a>
</li>
<li class="menuitem ">
<a href="index.html%3Ftranscript.html" data-shortcut="">
Transcript
</a>
</li>
</ul>
</nav>
</aside>
<main class="main-content">
<article class="article">
<h1>November 6th Lecture</h1>
<h2>Compare cont.</h2>
<p>Continuing from last week's topic, writing a function called <code>compare</code> in your custom class.</p>
<p>For example, for your project:</p>
<pre><code class="language-java">public bool compare(ResearchProject right, int type) {
if (type == 0) { // Is last lastname greater than right lastname alphabeticallly
if (lastname.compareTo(right.lastname) &gt; 0) {
return true;
} else {
return false;
}
} else if (type == 1) { // Implement a different type of comparison
}
}</code></pre>
<p>You can then use the sorting algorithms discussed previously to sort the ResearchProjects using the <code>compare</code> method.</p>
<h2>File IO</h2>
<p>First start by importing the required libraries. </p>
<pre><code class="language-java">import java.io.FileInputStream;
import java.io.IOException; // For error handling
import java.io.FileNotFoundException; // For error handling
import java.util.Scanner;</code></pre>
<p>Then inside your main method which throws an IOException (see example &quot;Reading the filename from the keyboard&quot;)</p>
<pre><code class="language-java">FileStream file;
Scanner in;
try { // Try something
file = new FileInputStream("test.txt");
in = new Scanner(file);
} catch (FileNotFoundException e) { // Catch IF it fails
System.out.println("File not found.");
in = new Scanner(System.in); // Read from the keyboard instead
}
// Read the file now....
String name = in.nextLine();</code></pre>
<p>Before we had linked the Scanner to the keyboard, now we're doing it to a file.</p>
<h3>Try-Catch</h3>
<p>In the code above you see what's called a try-catch block. That means that if something was to happen in the execution of the code. Instead of just crashing as we have been seeing normally. We can deal with the error. In the example above you saw that the program reads from the keyboard instead of the file</p>
<h3>Reading the filename from the keyboard</h3>
<pre><code class="language-java">public static void main(String[] args) throws IOException {
FileInputStream file;
Scanner in, scnr;
// Connect one scanner to the keyboard to get the filename
scnr = new Scanner(System.in);
System.out.print("Enter file name: ");
String filename = in.nextLine();
// Repeat code from previous example
FileStream file;
Scanner in;
try {
file = new FileInputStream(filename); // Only this line changed
in = new Scanner(file);
} catch (FileNotFoundException e) {
System.out.println("File not found.");
in = new Scanner(System.in);
}
String name = in.nextLine();
// ....
}</code></pre>
<p>The main throws the IOException since we don't deal with it in any of the catch blocks.</p>
<h3>Reading names of people from a file into an array</h3>
<p>Let's say we have a file with the following in it</p>
<pre><code>3
Paul
Peter
Mary</code></pre>
<p>3 is to indicate the number of names in th file.</p>
<p>Let's write code in order to read these names into an array!</p>
<pre><code class="language-java">import java.io.FileInputStream;
import java.io.IOException;
import java.io.FileNotFoundException;
import java.util.Scanner;
public static void main(String[] args) throws IOException {
FileInputStream file;
Scanner in, scnr;
scnr = new Scanner(System.in);
System.out.print("Enter file name: ");
String filename = in.nextLine();
FileStream file;
Scanner in;
try {
file = new FileInputStream(filename); // Only this line changed
in = new Scanner(file);
} catch (FileNotFoundException e) {
System.out.println("File not found.");
in = new Scanner(System.in);
}
// For the size of the array, get the first number in the file
int size = in.nextInt();
String[] nameArray = new String[size];
for (int i = 0; i &lt; size; i++) {
namesArray[i] = in.nextLine();
}
// Now all the names in the file is in the namesArray
}</code></pre>
</article>
</main>
<script src="themes/bitsandpieces/scripts/highlight.js"></script>
<script src="themes/bitsandpieces/scripts/mousetrap.min.js"></script>
<script type="text/x-mathjax-config">
MathJax.Hub.Config({
tex2jax: {
inlineMath: [ ['$','$'], ["\\(","\\)"] ],
processEscapes: true
}
});
</script>
<script type="text/javascript"
src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML">
</script>
<script>
hljs.initHighlightingOnLoad();
document.querySelectorAll('.menuitem a').forEach(function(el) {
if (el.getAttribute('data-shortcut').length > 0) {
Mousetrap.bind(el.getAttribute('data-shortcut'), function() {
location.assign(el.getAttribute('href'));
});
}
});
</script>
</body>
</html>