Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Ding;le
- *
- * @author Ulizio
- * @version 1
- */
- import java.io.*;
- import java.util.Scanner;
- public class StringArraySearch
- {
- public static void main(String [] args) {
- int size = 0;
- size = lengthFinder();
- String[] words = new String [size];
- arrayPrepper(words);
- arrayPrinter(words);
- System.out.println("");
- System.out.println("Hello. My name is Jeff. Please pick one of the above words.");
- System.out.println("I will try to guess your word.");
- /**
- * System.out.println("If your word is higher than my guess, type h.");
- * System.out.println("If your word is lower than my guess, type l.");
- */
- int guessIndex = guess(size, 0);
- int max = size;
- int min = 0;
- int roundCount = 0;
- while(true) {
- int currentGuess = guess(max, min);
- System.out.println(words[currentGuess]);
- boolean finished = check(currentGuess);
- if(finished == false) {
- char highOrLow = highLow();
- if(highOrLow == 'h') {
- max = (max + min)/2;
- }
- else if(highOrLow == 'l') {
- min = (max + min)/2;
- }
- else {
- }
- }
- if(finished == true) {
- System.out.println("I guessed your String in " + roundCount + " rounds!");
- break;
- }
- else {
- }
- roundCount++;
- }
- }
- public static void arrayPrepper(String [] words) {
- try {
- Scanner scan = new Scanner(new BufferedReader(new FileReader("words.txt")));
- int size = 0;
- while(scan.hasNext()) {
- size++;
- scan.next();
- }
- scan.close();
- scan = new Scanner(new BufferedReader(new FileReader("words.txt")));
- int count = 0;
- while(scan.hasNext()) {
- words[count] = scan.next();
- count++;
- }
- String myWord = "Phil";
- if (myWord.compareToIgnoreCase(words[10]) < 0) {
- /**
- * This means myWord is less than words[10]
- */
- }
- } catch (Exception e) {
- System.out.println("WAT: " + e.getMessage());
- }
- }
- public static int lengthFinder() {
- int y = 0;
- try {
- Scanner scan = new Scanner(new BufferedReader(new
- FileReader("words.txt")));
- //scan.useDelimiter(" ");
- //The delimiter is a coma with no space, so we will sep. nums with just a comma.
- //Create and prep the scanner
- while(scan.hasNext() == true) {
- y++;
- System.out.println(scan.nextDouble());
- }
- }
- catch (Exception e) {
- System.out.println(e.getMessage());
- }
- return(y);
- }
- public static void arrayPrinter(String [] words) {
- int size = words.length;
- for(int i = 0; i < size; i++) {
- System.out.println(i);
- }
- }
- public static int guess(int max, int min) {
- /**
- * This method calculates Jeff's guess.
- */
- int sumMaxMin = max + min;
- int guess = sumMaxMin/2;
- return(guess);
- }
- public static char highLow() {
- /**
- * This method asks the user if the guess was high or low.
- */
- Scanner scan = new Scanner(System.in);
- System.out.println("If my guess was too high, type h. If it was too low, type l.");
- char userInput = scan.next().charAt(0);
- return(userInput);
- }
- public static boolean check(int guess) {
- /**
- * This method checks the computer's guess.
- */
- Scanner scan = new Scanner(System.in);
- System.out.println("Was I correct? Type y for yes or n for no.");
- char userInput = scan.next().charAt(0);
- boolean finished = false;
- if(userInput == 'y') {
- finished = true;
- }
- else if(userInput == 'n') {
- finished = false;
- }
- else {
- }
- return(finished);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement