Advertisement
NicholasCSW

StringArraySearch

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