Advertisement
NicholasCSW

StringArraySearchV3

Mar 26th, 2017
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.00 KB | None | 0 0
  1. /**
  2.  * Ding;le
  3.  *
  4.  * @author Ulizio
  5.  * @version 1
  6.  */
  7. import java.io.File;
  8. import java.io.FileNotFoundException;
  9. import java.io.*;
  10. import java.util.Scanner;
  11. public class StringArraySearch
  12. {
  13.     public static void main(String [] args) {
  14.        
  15.         int max = 0;
  16.         int size = 0;
  17.         int min = 0;
  18.         int roundCount = 0;
  19.        
  20.         System.out.println("");
  21.         System.out.println("Hello. My name is Jeff from THE JEFF STORE.");
  22.         System.out.println("I will tell you if your word is in stock today.");
  23.        
  24.         try {
  25.         Scanner scan = new Scanner(new BufferedReader(new FileReader("words.txt")));
  26.        
  27.         while(scan.hasNext()) {
  28.              size++;
  29.              scan.next();
  30.         }
  31.         String[] words = new String [size];
  32.         scan.close();
  33.        
  34.         arrayPrepper(words);
  35.        
  36.         max = words.length;
  37.        
  38.         String userWord = getUserWord();
  39.        
  40.         int steps = 0;
  41.        
  42.         while(true) {
  43.            /**
  44.             * Time to binary search!
  45.             */
  46.            
  47.            int currentGuess = guess(max, min);
  48.            
  49.            //System.out.println(words[currentGuess]);
  50.            
  51.            boolean finished = check(userWord ,currentGuess, words);
  52.            if(finished == false) {
  53.                
  54.                char highOrLow = highLow(words, currentGuess, userWord);
  55.                
  56.                if(highOrLow == 'h') {
  57.                    
  58.                    min = (max + min)/2;
  59.                    
  60.                    steps++;
  61.                }
  62.                else if(highOrLow == 'l') {
  63.                    
  64.                    max = (max + min)/2;
  65.                    
  66.                    steps++;
  67.                }
  68.                else {
  69.                
  70.                }
  71.                
  72.                if(steps == words.length) {
  73.                    
  74.                    System.out.println("Your word is not in the array.");
  75.                    
  76.                    break;
  77.                    
  78.                 }
  79.                
  80.            }
  81.            
  82.            if(finished == true) {
  83.                
  84.                System.out.println("I guessed your String in " + steps + " rounds!");
  85.                
  86.                break;
  87.            }
  88.            else {
  89.                
  90.            }
  91.            
  92.            roundCount++;
  93.            
  94.            
  95.            
  96.         }
  97.        
  98.        
  99.        
  100.        }
  101.        catch(Exception e) {
  102.            
  103.        }
  104.        
  105.        
  106.        
  107.     }
  108.    
  109.     public static void arrayPrepper(String [] words) {
  110.        
  111.         try {
  112.            
  113.             Scanner scan = new Scanner(new BufferedReader(new FileReader("words.txt")));
  114.             int count = 0;
  115.             while(scan.hasNext()) {
  116.                 words[count] = scan.next();
  117.                 count++;
  118.             }
  119.          
  120.            
  121.            
  122.             String myWord = "Phil";
  123.            
  124.             if (myWord.compareToIgnoreCase(words[10]) < 0) {
  125.                 /**
  126.                  * This means myWord is less than words[10]
  127.                  */
  128.             }
  129.            
  130.            
  131.            
  132.            
  133.         } catch (Exception e) {
  134.             System.out.println("WAT: " + e.getMessage());
  135.         }
  136.        
  137.     }
  138.    
  139.     public static int lengthFinder() {
  140.        
  141.         int y = 0;
  142.        
  143.         try {
  144.             Scanner scan = new Scanner(new BufferedReader(new
  145.             FileReader("words.txt")));
  146.             scan.useDelimiter(",");
  147.             //The delimiter is a coma with no space, so we will sep. nums with just a comma.
  148.             //Create and prep the scanner
  149.             while(scan.hasNext() == true) {
  150.              y++;
  151.              //System.out.println(scan.nextDouble());
  152.             }
  153.         }
  154.        
  155.         catch (Exception e) {
  156.             System.out.println(e.getMessage());
  157.         }
  158.         return(y);
  159.        
  160.        
  161.     }
  162.    
  163.     public static void arrayPrinter(String [] words) {
  164.        
  165.         int size = words.length;
  166.        
  167.         for(int i = 0; i < size; i++) {
  168.            
  169.             System.out.println(words[i]);
  170.         }
  171.        
  172.     }
  173.    
  174.     public static int guess(int max, int min) {
  175.         /**
  176.          * This method calculates Jeff's guess.
  177.          */
  178.        
  179.         int sumMaxMin = max + min;
  180.        
  181.         int guess = sumMaxMin/2;
  182.        
  183.         return(guess);
  184.        
  185.     }
  186.    
  187.     public static char highLow(String [] words, int currentIndex, String userString) {
  188.         /**
  189.          * This method checks if the guess was too high or too low
  190.          */
  191.        
  192.         char c = 'y';
  193.         if(userString.compareTo(words[currentIndex]) < 0) {
  194.            
  195.             c = 'l';
  196.         }
  197.         else if(userString.compareTo(words[currentIndex]) == 0) {
  198.            
  199.             c = 'h';
  200.         }
  201.         else if(userString.equals(words[currentIndex])){
  202.            
  203.         }
  204.        
  205.         return(c);
  206.        
  207.     }
  208.    
  209.     public static boolean check(String userString, int currentIndex, String [] words) {
  210.         /**
  211.          * This method checks the computer's guess. If correct, finished. If not, not finished
  212.          */
  213.        
  214.        
  215.         boolean finished = false;
  216.         if(userString.compareTo(words[currentIndex]) < 0) {
  217.            
  218.             finished = false;
  219.         }
  220.         else if(userString.compareTo(words[currentIndex]) == 0) {
  221.            
  222.             finished = false;
  223.         }
  224.         else {
  225.             finished = true;
  226.            
  227.             System.out.println("Your word has been found.");
  228.         }
  229.        
  230.         return(finished);
  231.        
  232.     }
  233.    
  234.     public static String getUserWord(){
  235.        
  236.         Scanner scan3 = new Scanner(System.in);
  237.        
  238.         System.out.println("Please type your word.");
  239.        
  240.         String word = scan3.next();
  241.        
  242.         return(word);
  243.        
  244.     }
  245.    
  246.     public static void setupArray() {
  247.        
  248.        
  249.        
  250.        
  251.     }
  252. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement