80 lines
3.2 KiB
Java
80 lines
3.2 KiB
Java
/*
|
|
* 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 lab10_calebfontenot;
|
|
|
|
import java.util.Scanner;
|
|
|
|
/**
|
|
*
|
|
* @author caleb
|
|
*/
|
|
public class CharacterClassStringClass {
|
|
|
|
public static void main(String[] args) {
|
|
// Create Scanner
|
|
Scanner input = new Scanner(System.in);
|
|
|
|
// Declare variables
|
|
String userResponse;
|
|
boolean loopToggle = true;
|
|
int parseCheck;
|
|
|
|
// While loop
|
|
while (loopToggle) {
|
|
System.out.println("Continue looping?");
|
|
System.out.println("Please enter y/n or yes/no: ");
|
|
userResponse = input.next();
|
|
|
|
if (userResponse.toLowerCase().charAt(0) == 'y') {
|
|
if (userResponse.length() > 1) { // User has entered a string that starts with y
|
|
// Check if string == yes
|
|
if (userResponse.toLowerCase().equals("yes")) {
|
|
System.out.println("You typed '" + userResponse + '\'');
|
|
System.out.println("ok!");
|
|
} else {
|
|
System.out.println("Invalid input! You entered '" + userResponse + '\'');
|
|
}
|
|
} else {
|
|
// User has responded with 'y'
|
|
System.out.println("You typed '" + userResponse + '\'');
|
|
System.out.println("ok!");
|
|
}
|
|
} else if (userResponse.toLowerCase().charAt(0) == 'n') {
|
|
if (userResponse.length() > 1) { // User has entered a string that starts with y
|
|
// Check if string == yes
|
|
if (userResponse.toLowerCase().equals("no")) {
|
|
System.out.println("You typed '" + userResponse + '\'');
|
|
// Kill the loop.
|
|
loopToggle = false;
|
|
} else {
|
|
System.out.println("Invalid input! You entered '" + userResponse + '\'');
|
|
}
|
|
} else {
|
|
// User has responded with 'n'
|
|
System.out.println("You typed " + userResponse.charAt(0));
|
|
// Kill the loop.
|
|
loopToggle = false;
|
|
}
|
|
} else { // Invalid input handling
|
|
// Attempt to parse string as an integer.
|
|
try {
|
|
parseCheck = Integer.parseInt(userResponse);
|
|
// If it got past this, the user entered a number.
|
|
System.out.println("Invalid input! You entered " + parseCheck + ", which is an integer!");
|
|
} catch (NumberFormatException ex) {
|
|
if (!(userResponse.length() > 1)) { // Condition is true if response is a char.
|
|
System.out.println("'" + userResponse + "' is an incorrect character!");
|
|
} else { // Response is a string.
|
|
System.out.println("'" + userResponse + "' is too long!");
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
}
|