/**
* Allow you to create and see your EAUsername by inputting your name and graduation year
*
* @author Sameer Saxena
* @version 1.0
*/
import java.util.*;
public class EAUsername
{
public static void main(String args[]){
//Create input for name
Scanner scan = new Scanner (System.in);
String name = "";
while(true) {
//ERROR HANDLING - If they enter a number instead of a name
System.out.println("Please enter your name (First Middle Last): ");
name = scan.nextLine();
try {
int number = Integer.parseInt(name);
System.out.println("Please enter an actual name, not a number. ");
} catch (NumberFormatException error) {
break;
}
}
//Initializing variables
int firstSpace = name.indexOf(" ");
boolean noMiddleName = false;
int secondSpace = name.indexOf(" ", firstSpace + 1);
if (secondSpace < 0) {
noMiddleName = true;
}
//Create first name out of name input
String firstName = name.substring(0, firstSpace);
String firstNameUser = firstName.substring(0, 1);
firstNameUser = firstNameUser.toLowerCase();
//Create middle name out of name input
String middleName = "";
if (noMiddleName) {
middleName = name.substring((firstSpace + 1), name.length());
} else {
middleName = name.substring((firstSpace + 1), secondSpace);
}
String middleNameUser = middleName.substring(0, 1);
middleNameUser = middleNameUser.toLowerCase();;
//Create last name out of name input
String lastName = "";
if (!noMiddleName) {
//ERROR HANDLING - if middle name not input or last name not 4 characters
lastName = name.substring((secondSpace + 1), name.length());
if (lastName.length() >= 4) {
String lastNameUser = lastName.substring(0, 4);
lastName = lastNameUser;
}
lastName = lastName.toLowerCase();
} else {
if (middleName.length() >= 4) {
lastName = middleName.substring(0,4);
} else {
lastName = middleName;
}
middleNameUser = "";
}
//Create graduation year input
System.out.println("Enter your graduation year: ");
String gradYear = scan.nextLine();
String gradYearUser = gradYear.substring(2, 4);
//Set username and print it out to screen
String username = lastName + firstNameUser + middleNameUser + gradYearUser;
System.out.println("Your username is: " + username.toLowerCase());
}
}
Be the first to comment
You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.