108 lines
8.9 KiB
HTML
108 lines
8.9 KiB
HTML
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
|
|
<html>
|
|
<head>
|
|
<title>CharacterClassStringClass.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}
|
|
.string {color: #6a8759}
|
|
.number {color: #6897bb}
|
|
.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}
|
|
.ST0 {color: #287bde}
|
|
.literal {color: #cc7832}
|
|
.ST3 {font-family: monospace; font-weight: bold; font-style: italic}
|
|
-->
|
|
</style>
|
|
</head>
|
|
<body>
|
|
<table width="100%"><tr><td align="center">/home/caleb/ASDV-Java/Assignments/Lab10_CalebFontenot/src/lab10_calebfontenot/CharacterClassStringClass.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> lab10_calebfontenot;
|
|
|
|
<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> CharacterClassStringClass {
|
|
|
|
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">void</span> <span class="ST1">main</span>(String[] args) {
|
|
<span class="comment">// Create Scanner</span>
|
|
Scanner input = <span class="literal">new</span> Scanner(System.<span class="ST2">in</span>);
|
|
|
|
<span class="comment">// Declare variables</span>
|
|
String userResponse;
|
|
<span class="literal">boolean</span> loopToggle = <span class="literal">true</span>;
|
|
<span class="literal">int</span> parseCheck;
|
|
|
|
<span class="comment">// While loop</span>
|
|
<span class="literal">while</span> (loopToggle) {
|
|
System.<span class="ST2">out</span>.println(<span class="string">"</span><span class="string">Continue looping?</span><span class="string">"</span>);
|
|
System.<span class="ST2">out</span>.println(<span class="string">"</span><span class="string">Please enter y/n or yes/no: </span><span class="string">"</span>);
|
|
userResponse = input.next();
|
|
|
|
<span class="literal">if</span> (userResponse.toLowerCase().charAt(<span class="number">0</span>) == <span class="string">'</span><span class="string">y</span><span class="string">'</span>) {
|
|
<span class="literal">if</span> (userResponse.length() > <span class="number">1</span>) { <span class="comment">// User has entered a string that starts with y</span>
|
|
<span class="comment">// Check if string == yes</span>
|
|
<span class="literal">if</span> (userResponse.toLowerCase().equals(<span class="string">"</span><span class="string">yes</span><span class="string">"</span>)) {
|
|
System.<span class="ST2">out</span>.println(<span class="string">"</span><span class="string">You typed '</span><span class="string">"</span> + userResponse + <span class="string">'</span><span class="string">\'</span><span class="string">'</span>);
|
|
System.<span class="ST2">out</span>.println(<span class="string">"</span><span class="string">ok!</span><span class="string">"</span>);
|
|
} <span class="literal">else</span> {
|
|
System.<span class="ST2">out</span>.println(<span class="string">"</span><span class="string">Invalid input! You entered '</span><span class="string">"</span> + userResponse + <span class="string">'</span><span class="string">\'</span><span class="string">'</span>);
|
|
}
|
|
} <span class="literal">else</span> {
|
|
<span class="comment">// User has responded with 'y'</span>
|
|
System.<span class="ST2">out</span>.println(<span class="string">"</span><span class="string">You typed '</span><span class="string">"</span> + userResponse + <span class="string">'</span><span class="string">\'</span><span class="string">'</span>);
|
|
System.<span class="ST2">out</span>.println(<span class="string">"</span><span class="string">ok!</span><span class="string">"</span>);
|
|
}
|
|
} <span class="literal">else</span> <span class="literal">if</span> (userResponse.toLowerCase().charAt(<span class="number">0</span>) == <span class="string">'</span><span class="string">n</span><span class="string">'</span>) {
|
|
<span class="literal">if</span> (userResponse.length() > <span class="number">1</span>) { <span class="comment">// User has entered a string that starts with y</span>
|
|
<span class="comment">// Check if string == yes</span>
|
|
<span class="literal">if</span> (userResponse.toLowerCase().equals(<span class="string">"</span><span class="string">no</span><span class="string">"</span>)) {
|
|
System.<span class="ST2">out</span>.println(<span class="string">"</span><span class="string">You typed '</span><span class="string">"</span> + userResponse + <span class="string">'</span><span class="string">\'</span><span class="string">'</span>);
|
|
<span class="comment">// Kill the loop.</span>
|
|
loopToggle = <span class="literal">false</span>;
|
|
} <span class="literal">else</span> {
|
|
System.<span class="ST2">out</span>.println(<span class="string">"</span><span class="string">Invalid input! You entered '</span><span class="string">"</span> + userResponse + <span class="string">'</span><span class="string">\'</span><span class="string">'</span>);
|
|
}
|
|
} <span class="literal">else</span> {
|
|
<span class="comment">// User has responded with 'n'</span>
|
|
System.<span class="ST2">out</span>.println(<span class="string">"</span><span class="string">You typed </span><span class="string">"</span> + userResponse.charAt(<span class="number">0</span>));
|
|
<span class="comment">// Kill the loop.</span>
|
|
loopToggle = <span class="literal">false</span>;
|
|
}
|
|
} <span class="literal">else</span> { <span class="comment">// Invalid input handling</span>
|
|
<span class="comment">// Attempt to parse string as an integer.</span>
|
|
<span class="literal">try</span> {
|
|
parseCheck = Integer.<span class="ST3">parseInt</span>(userResponse);
|
|
<span class="comment">// If it got past this, the user entered a number.</span>
|
|
System.<span class="ST2">out</span>.println(<span class="string">"</span><span class="string">Invalid input! You entered </span><span class="string">"</span> + parseCheck + <span class="string">"</span><span class="string">, which is an integer!</span><span class="string">"</span>);
|
|
} <span class="literal">catch</span> (NumberFormatException ex) {
|
|
<span class="literal">if</span> (!(userResponse.length() > <span class="number">1</span>)) { <span class="comment">// Condition is true if response is a char.</span>
|
|
System.<span class="ST2">out</span>.println(<span class="string">"</span><span class="string">'</span><span class="string">"</span> + userResponse + <span class="string">"</span><span class="string">' is an incorrect character!</span><span class="string">"</span>);
|
|
} <span class="literal">else</span> { <span class="comment">// Response is a string.</span>
|
|
System.<span class="ST2">out</span>.println(<span class="string">"</span><span class="string">'</span><span class="string">"</span> + userResponse + <span class="string">"</span><span class="string">' is too long!</span><span class="string">"</span>);
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
}
|
|
|
|
</pre></body>
|
|
</html>
|