Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Ding;le
- *
- * @author Ulizio
- * @version 1
- */
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.io.*;
- import java.util.Scanner;
- public class StringArraySearch
- {
- public static void main(String [] args) {
- int max = 0;
- int size = 0;
- int min = 0;
- int roundCount = 0;
- System.out.println("");
- System.out.println("Hello. My name is Jeff from THE JEFF STORE.");
- System.out.println("I will tell you if your word is in stock today.");
- try {
- Scanner scan = new Scanner(new BufferedReader(new FileReader("words.txt")));
- while(scan.hasNext()) {
- size++;
- scan.next();
- }
- String[] words = new String [size];
- scan.close();
- arrayPrepper(words);
- max = words.length;
- String userWord = getUserWord();
- int steps = 0;
- while(true) {
- /**
- * Time to binary search!
- */
- int currentGuess = guess(max, min);
- //System.out.println(words[currentGuess]);
- boolean finished = check(userWord ,currentGuess, words);
- if(finished == false) {
- char highOrLow = highLow(words, currentGuess, userWord);
- if(highOrLow == 'h') {
- min = (max + min)/2;
- steps++;
- }
- else if(highOrLow == 'l') {
- max = (max + min)/2;
- steps++;
- }
- else {
- }
- if(steps == words.length) {
- System.out.println("Your word is not in the array.");
- break;
- }
- }
- if(finished == true) {
- System.out.println("I guessed your String in " + steps + " rounds!");
- break;
- }
- else {
- }
- roundCount++;
- }
- }
- catch(Exception e) {
- }
- }
- public static void arrayPrepper(String [] words) {
- try {
- Scanner 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(words[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(String [] words, int currentIndex, String userString) {
- /**
- * This method checks if the guess was too high or too low
- */
- char c = 'y';
- if(userString.compareTo(words[currentIndex]) < 0) {
- c = 'l';
- }
- else if(userString.compareTo(words[currentIndex]) == 0) {
- c = 'h';
- }
- else if(userString.equals(words[currentIndex])){
- }
- return(c);
- }
- public static boolean check(String userString, int currentIndex, String [] words) {
- /**
- * This method checks the computer's guess. If correct, finished. If not, not finished
- */
- boolean finished = false;
- if(userString.compareTo(words[currentIndex]) < 0) {
- finished = false;
- }
- else if(userString.compareTo(words[currentIndex]) == 0) {
- finished = false;
- }
- else {
- finished = true;
- System.out.println("Your word has been found.");
- }
- return(finished);
- }
- public static String getUserWord(){
- Scanner scan3 = new Scanner(System.in);
- System.out.println("Please type your word.");
- String word = scan3.next();
- return(word);
- }
- public static void setupArray() {
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement