/**
* Gererated a username in the EA format
*
* @author Sean Appaneal
*
* @version v1.0
*
*/
import java.util.*;
import java.util.Scanner;
public class UsernameGenerator
{
public static void main(String[] args) {
try{ //try catch, cause theres one thing that breaks everything if the user is stupid
boolean keepGoing = true;
while(keepGoing){//while loop to allow redoing without restarting
Scanner sc = new Scanner(System.in);
//naming variables
String fullName;
String fName;
String mName;
String lName;
String nameLength;
String gradYear;
System.out.println("Please input your full name");
fullName = sc.nextLine();
int spot = fullName.indexOf(" ");//find first space
fName = fullName.substring(0,1);//get first letter of first name
mName = fullName.substring(spot+1,spot+2);//get first letter of middle name
fullName = fullName.substring(spot+1);//chop of everything before and including first space
spot = fullName.indexOf(" ");//find second space
lName = fullName.substring(spot+1); //get full last name
if(spot == -1){//if theres only one sapce, they don't have a middle name, so this gets rid of it
mName = "";
}
int length = lName.length();//gets length of last name
if (length < 4){ //for if the last name if < 4 characters
lName = fullName.substring(spot+1);
}else{
lName = fullName.substring(spot+1, spot+5); //gets first for letters of last names > 4 chars
}
System.out.println("Please enter your graduation year");//get grad year
gradYear = sc.nextLine();
length = gradYear.length();
if(length < 4 || length > 4){
System.out.println("Please use only four digits");
gradYear = sc.nextLine();
}
gradYear = gradYear.substring(2,4);//take last two numbers of grad year
System.out.println(lName + fName + mName + gradYear);//print the results
//ask if they want to go again
System.out.println("Press 1 to continue, or 2 to stop");
int go = sc.nextInt();
if(go != 1)//stop if user doesn't input "1"
keepGoing = false;
}//end of while loop
}//end of try
catch(Exception e){
/*sarcastic comment about user putting in the wrong number of gradYear digits multiple times in a row,
which for some reason breaks the program
*/
System.out.println("Congrats you managed to break it");
}
}//end of main
}//end of class
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.