Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- /**
- * Author: Andrew Wagner
- * Project: ExploratoryProject
- * Date: 7/29/13
- * Time: 9:19 PM
- */
- public class RunMain {
- public static void main(String args[]) {
- //Question/Command Strings
- String enterName = "What is your name?";
- String enterGender = "Are you male or female? (Type 1 for Male, or 2 for Female)\n1. Male\n2. Female";
- String enterAge = "How old are you?";
- String enterColors = "What are three of your favorite colors?";
- String enterColorOne = "Color One: ";
- String enterColorTwo = "Color Two: ";
- String enterColorThree = "Color Three: ";
- String enterColorFour = "Color Four: ";
- String enterColorFive = "Color Five: ";
- String queryQuestion = "Would you like to perform a search? Type \"y\" for yes, or \"n\" for no.";
- String queryPrompt = "Search for user-inputted color: ";
- //Return data Strings
- String nameIs = "Your name is ";
- String genderIs = "You are a ";
- String ageIs = "You are ";
- String yrs = " yrs old";
- String colorsAre = "Your favorite colors are: ";
- String yourQueriedColor = "Your queried color, ";
- String colorFoundAtPos = ", was found at position ";
- //Error messages
- String noGender = "Your gender could not be determined... o.O";
- String noColor = ", was not found";
- //Loading messages
- String genderSorting = "Sorting favorite colors alphabetically...";
- //Misc
- String boy = "boy";
- String girl = "girl";
- String period = ".";
- String exclaimation = "!";
- String termProgram = "Ok, terminating program";
- Scanner scanner;
- //Input values
- String name;
- int genderInput;
- int age;
- String colorOne;
- String colorTwo;
- String colorThree;
- String colorFour;
- String colorFive;
- //Output values
- String gender;
- //Sorting vars
- int index;
- boolean swapped = false;
- //Search vars
- String queryConfirm;
- String colorQuery;
- int min = 0;
- int max;
- int med;
- int returnIndex = -1;
- scanner = new Scanner(System.in);
- //Ask for user input
- System.out.println(enterName);
- name = scanner.nextLine();
- System.out.println(enterGender);
- genderInput = scanner.nextInt();
- switch(genderInput) {
- case 1:
- gender = boy;
- break;
- case 2:
- gender = girl;
- break;
- default:
- gender = "";
- break;
- }
- System.out.println(enterAge);
- age = scanner.nextInt();
- System.out.println(enterColors);
- System.out.println(enterColorOne);
- scanner.nextLine();
- colorOne = scanner.nextLine();
- System.out.println(enterColorTwo);
- colorTwo = scanner.nextLine();
- System.out.println(enterColorThree);
- colorThree = scanner.nextLine();
- System.out.println(enterColorFour);
- colorFour = scanner.nextLine();
- System.out.println(enterColorFive);
- colorFive = scanner.nextLine();
- String[] colors = {colorOne, colorTwo, colorThree, colorFour, colorFive};
- //Return user input
- System.out.println(nameIs + name + period);
- if(!gender.equals("")) {
- System.out.println(genderIs + gender + period);
- } else {
- System.out.println(noGender);
- }
- //Color sorting alphabetically
- System.out.println(genderSorting);
- do {
- swapped = false;
- for(index = 0; index < (colors.length - 1); index++) {
- if(colors[index].charAt(0) > colors[index + 1].charAt(0)) {
- String replaced = colors[index + 1];
- colors[index + 1] = colors[index];
- colors[index] = replaced;
- swapped = true;
- }
- }
- } while (swapped);
- //Return alphabetically sorted array
- System.out.println(colorsAre);
- for(index = 0; index < colors.length; index++) {
- System.out.println(colors[index]);
- }
- //Ask for query
- System.out.println(queryQuestion);
- queryConfirm = scanner.nextLine();
- //If yes, search for inputted color
- if(queryConfirm.toLowerCase().equals("y")) {
- System.out.println(queryPrompt);
- colorQuery = scanner.nextLine();
- max = colors.length;
- //TODO Check second, third, fourth, etc. letters + compare
- while(max >= min) {
- med = ((min + max) / 2);
- if(colors[med].charAt(0) > colorQuery.charAt(0)) {
- max = (med - 1);
- } else if (colors[med].charAt(0) < colorQuery.charAt(0)) {
- min = (med + 1);
- } else {
- max = -1;
- returnIndex = med;
- }
- }
- if(returnIndex != -1) {
- System.out.println(yourQueriedColor + colorQuery + colorFoundAtPos + (returnIndex + 1) + period);
- } else {
- System.out.println(yourQueriedColor + colorQuery + noColor + exclaimation);
- }
- //If no, print termination of program to console
- } else {
- System.out.println(termProgram + period);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement