daily pastebin goal
15%
SHARE
TWEET

Untitled

a guest Dec 13th, 2018 54 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.util.Scanner;
  2. import java.util.Arrays;
  3. import java.util.Random;
  4.  
  5. public class SentenceScramble {
  6.  
  7.     // the main method declares a random and sets an integer to a random value from 0 to 2
  8.     //6 different arrays are declared and assigned sentences with pairs of two that are identical
  9.     //a println statement gives the user instructions on how to play
  10.     //it uses the random integer to determine which sentence is used
  11.     //it sends one sentence to method scrambleArray and then sends the
  12.     //scrambled sentence, original sentence, and corresponding word to method acceptUser
  13.    
  14.     public static void main(String[] args) {
  15.         Random rand = new Random();
  16.         int determineArr = rand.nextInt(3);
  17.        
  18.         //declare sentences
  19.         String[] theTrueFirst = {"no ", "one ", "Cares ", "About ", "your ", "feeLings ", "except ", "your ", "moM ", "."};
  20.         String[] theTrueSecond = {"there ", "iS ", "a ", "lot ", "to ", "do ", "In ", "The ", "world ", "."};
  21.         String[] theTrueThird = {"life ", "maY ", "havE ", "no ", "purpose, ", "but ", "neither ", "doeS ","death ", "."};
  22.        
  23.         String[] theFirst = {"no ", "one ", "Cares ", "About ", "your ", "feeLings ", "except ", "your ", "moM ", "."};
  24.         String[] theSecond = {"there ", "iS ", "a ", "lot ", "to ", "do ", "In ", "The ", "world ", "."};
  25.         String[] theThird = {"life ", "maY ", "havE ", "no ", "purpose, ", "but ", "neither ", "doeS ","death ", "."};
  26.        
  27.         //give instructions to user
  28.         System.out.println("If you want to see your sentence, input \"LOOK\" \n"
  29.                 + "If you want to switch words in a sentnce, input \"SWAP int int\"\n"
  30.                 + "If you want to guess the word, input \"GUESS\" and then input your word\n"
  31.                 + "Enter \"quit\" if you want to stop\n\n");
  32.        
  33.         //uses random int to determine which sentence to use and scrambles it
  34.         //then sends it to method acceptUser
  35.         if (determineArr == 0) {
  36.             theFirst = scrambleArray(theFirst);
  37.             acceptUser(theFirst, theTrueFirst, "CALM");
  38.            
  39.         }else if(determineArr == 1) {
  40.             theSecond = scrambleArray(theSecond);
  41.             acceptUser(theSecond, theTrueSecond, "SIT");
  42.            
  43.         }else {
  44.             theThird = scrambleArray(theThird);
  45.             acceptUser(theThird, theTrueThird, "YES");
  46.            
  47.         }
  48.            
  49.     }
  50.     // scrambles the sentence by randomly swappng array index values 100 times
  51.     public static String[] scrambleArray (String[] newArr) {
  52.         for(int i = 0; i < 100; i++) {
  53.             Random sequence = new Random();
  54.             int swapOne = sequence.nextInt(9);
  55.             int swapTwo = sequence.nextInt(9);
  56.             String tempSavedValue = newArr[swapOne];
  57.             newArr[swapOne] = newArr[swapTwo];
  58.             newArr[swapTwo] = tempSavedValue;
  59.         }
  60.         return newArr;
  61.     }
  62.    
  63.     //accepts the scrambled sentence, original sentence, and the word the user is trying to find
  64.     //accepts user input for the action the user wants to take so long s they don't enter quit
  65.     public static void acceptUser(String[] sentence, String[] trueSentence, String word) {
  66.         Scanner user = new Scanner (System.in);
  67.        
  68.         //primes while loop
  69.         String userAction = "nothing";
  70.  
  71.         while(!userAction.equals("quit")) {
  72.             //accepts user input and sets it to uppercase
  73.             userAction = user.nextLine();
  74.             userAction = userAction.toUpperCase();
  75.            
  76.             //if the user inputs look, it prints the sentence
  77.             if(userAction.equals("LOOK")) {
  78.                 for(int i = 0; i < sentence.length; i++) {
  79.                     System.out.print(sentence[i]);
  80.                 }
  81.                 System.out.println("\n");
  82.            
  83.             //if the user inputs guess, it checks to see if the word is correct
  84.             //it either tells the user they won and stops the loop or tells the user they're wrong
  85.             }else if (userAction.equals("GUESS")) {
  86.                 String userGuess = user.nextLine();
  87.                 userGuess = userGuess.toUpperCase();
  88.                 if(userGuess.equals(word)) {
  89.                     System.out.println("Congrats, you won!");
  90.                     userAction = "quit";
  91.                    
  92.                 }else {
  93.                     System.out.println("Nope, try again.\n");
  94.                 }
  95.            
  96.             //if the user didn't type look or guess, it breaks apart the input into substrings
  97.             //substrings are checked to see if they are valid input
  98.             //if they are correct for swap, it swaps the ints and checks if the new sentence is in order
  99.             }else {
  100.                 if (userAction.length() == 8) {
  101.                     //breaks into substrings
  102.                     String modAction = userAction.substring(0, 4);
  103.                     String firstSpace = userAction.substring(4, 5);
  104.                     String firstSwap = userAction.substring(5, 6);
  105.                     String secondSpace = userAction.substring(6, 7);
  106.                     String secondSwap = userAction.substring(7, 8);
  107.                    
  108.                     // checks to see if ints are valid by sending substrings to checkSwap method
  109.                     //if they aren't, it tells the user and asks for new input
  110.                     Boolean errorCheck = true;
  111.                     errorCheck = checkSwap(firstSwap, secondSwap);
  112.                    
  113.                 if (errorCheck == false) {
  114.                         System.out.println("Error: invalid input\n");
  115.                 }else {
  116.                     //checks if their are spaces between the ints
  117.                     if(!firstSpace.equals(" ") || !secondSpace.equals(" ") || !firstSpace.equals(" ") && !secondSpace.equals(" ")) {
  118.                         System.out.println("Error: invalid input\n");
  119.                     }else {
  120.                         //checks if input equals swap
  121.                         if (modAction.equals("SWAP")) {
  122.                             int intFirstSwap = remade(firstSwap) - 1;
  123.                             int intSecondSwap = remade(secondSwap) - 1;
  124.                             String firstValue = sentence[intFirstSwap];
  125.                             sentence[intFirstSwap] = sentence[intSecondSwap];
  126.                             sentence[intSecondSwap]= firstValue;
  127.                            
  128.                             //checks if new sentence is in order
  129.                             Boolean check = false;
  130.                             if(Arrays.equals(sentence, trueSentence)) {
  131.                                 check = true;
  132.                             }
  133.                            
  134.                             if(check.equals(true)) {
  135.                                 System.out.println("Congrats! Your Sentence is in order.\n");
  136.                                 for(int i = 0; i < sentence.length; i++) {
  137.                                     System.out.print(sentence[i]);
  138.                                 }
  139.                                 System.out.println("\n");
  140.                                
  141.                             }else {
  142.                             System.out.println("Not quite, swap again.\n");
  143.                             }
  144.                            
  145.                         }else {
  146.                             System.out.println("Error: invalid input\n");
  147.                         }
  148.                     }
  149.                 }
  150.                    
  151.                 } else {
  152.                     System.out.println("Error: invalid input\n");
  153.                 }
  154.                
  155.             }
  156.             }
  157.        
  158.     }
  159.    
  160.     //checks if the ints input as SWAP int int are between 1 and 9
  161.     public static Boolean checkSwap (String firstInt, String secondInt) {
  162.         if ( !firstInt.equals("1") && !firstInt.equals("2") && !firstInt.equals("3") && !firstInt.equals("4") && !firstInt.equals("5") &&
  163.              !firstInt.equals("6") && !firstInt.equals("7") && !firstInt.equals("8") && !firstInt.equals("9")) {
  164.                 return false;
  165.         } else if ( !secondInt.equals("1") && !secondInt.equals("2") && !secondInt.equals("3") && !secondInt.equals("4") && !secondInt.equals("5") &&
  166.                     !secondInt.equals("6") && !secondInt.equals("7") && !secondInt.equals("8") && !secondInt.equals("9")) {
  167.                         return false;
  168.         } else {
  169.             return true;
  170.         }
  171.     }
  172.      
  173.     //turns strings into ints
  174.     public static int remade(String str) {
  175.         int change;
  176.         Scanner look = new Scanner(str);
  177.         change = look.nextInt();
  178.         look.close();
  179.         return change;
  180.        
  181.     }
  182.  
  183. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top