Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2018
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.99 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package substringfinder;
  7.  
  8. import java.util.Scanner;
  9.  
  10. /**
  11.  *
  12.  * @author jussi
  13.  */
  14. public class SubstringFinder
  15. {
  16.  
  17.     /**
  18.      * @param args the command line arguments
  19.      */
  20.     static String input = "";
  21.     static String subString = "";
  22.     static char jokerPosition = ' ';
  23.  
  24.     public static void main(String[] args)
  25.     {
  26.         Scanner reader = new Scanner(System.in);
  27.         boolean running = true;
  28.         System.out.println("Hello! I find substrings.");
  29.  
  30.         while (running)
  31.         {
  32.             // go through the reader until correct input is taken
  33.             getCorrectInput(reader);
  34.             int iterator = 0;
  35.             int length = subString.length();
  36.            
  37.             // if the joker position is a the start of subString iterate the loop to one
  38.             if (jokerPosition == 's')
  39.             {
  40.                 iterator = 1;
  41.             }
  42.             // if the joker position is at the end of subString shorten the loop by one
  43.             if (jokerPosition == 'e')
  44.             {
  45.                 length = subString.length() - 1;
  46.             }
  47.  
  48.             String output = "";
  49.             for (int i = 0; i < input.length(); i++)
  50.             {
  51.                 // make dummy output which we can rely on
  52.                 String iterationOutput = output;
  53.                 // make dummy iterator in which we can rely on
  54.                 int iteratorCheck = i;
  55.                 // if the characters match go into loop
  56.                 if (input.charAt(i) == subString.charAt(iterator))
  57.                 {
  58.                    
  59.                     boolean found = false;
  60.                     // if we have joker position currently or there would be theoritically possibility of being a match go into secondary loop
  61.                     if (((i == 0 && jokerPosition == 's') || (i == input.length() - length && jokerPosition == 'e') || jokerPosition == ' ') && i <= input.length() - length)
  62.                     {
  63.                         // here we iterate both of the loops at the same time
  64.                         for (int j = iterator; j < length; j++, i++)
  65.                         {
  66.                             // if we find a match add the letter to the output
  67.                             if (input.charAt(i) == subString.charAt(j))
  68.                             {
  69.                                 found = true;
  70.                                 output += input.charAt(i);
  71.                             } else
  72.                                 //otherwise break out of the loop
  73.                             {
  74.                                 found = false;
  75.                                 break;
  76.                             }
  77.                         }
  78.                         //if we find a match we print it out
  79.                         if (found)
  80.                         {
  81.                         //iterate the i with empty spaces until it's the lenght of the original input
  82.                         while (i <= input.length() - 1)
  83.                         {
  84.                            
  85.                             output += "-";
  86.                             i++;
  87.                         }
  88.                             System.out.println(output);
  89.                         }
  90.                         // now we reset the output to the original state of the current loop
  91.                         output = iterationOutput;
  92.                     }
  93.                 }
  94.                 output += "-";
  95.                 // and now we reset the iterator to original state of the current loop
  96.                 i = iteratorCheck;
  97.             }
  98.             boolean continueCheck = false;
  99.             String choice = "";
  100.            
  101.             // check if the user wants to continue
  102.             while (!continueCheck)
  103.             {
  104.                 System.out.println("Continue (y/n)?");
  105.                 choice = reader.nextLine();
  106.                 switch (choice)
  107.                 {
  108.                     case "y":
  109.                         continueCheck = true;
  110.                         break;
  111.                     case "n":
  112.                         continueCheck = true;
  113.                         running = false;
  114.                         System.out.println("See you soon.");
  115.                         break;
  116.                     default:
  117.                         System.out.println("Error!");
  118.                         break;
  119.                 }
  120.             }
  121.  
  122.         }
  123.     }
  124.     //basically just checking if the input is correct and if there is joker (*) in place
  125.     public static void getCorrectInput(Scanner aReader)
  126.     {
  127.         Scanner reader = aReader;
  128.         boolean correctInput = false;
  129.  
  130.         while (!correctInput)
  131.         {
  132.             input = "";
  133.             subString = "";
  134.             jokerPosition = ' ';
  135.             System.out.println("Please, enter a string:");
  136.             input = reader.nextLine();
  137.             System.out.println("Please, enter a substring");
  138.             subString = reader.nextLine();
  139.             int inputLength = input.length();
  140.             int subStringLength = subString.length();
  141.  
  142.             if (inputLength > 0 && subStringLength > 0 && !(subString.equals("*")))
  143.             {
  144.                 if (subString.charAt(0) == '*' || subString.charAt(subStringLength - 1) == '*')
  145.                 {
  146.                     subStringLength--;
  147.  
  148.                     if (subString.charAt(0) == '*')
  149.                     {
  150.                         jokerPosition = 's';
  151.                     } else
  152.                     {
  153.                         jokerPosition = 'e';
  154.                     }
  155.  
  156.                 }
  157.                 if (subStringLength < inputLength)
  158.                 {
  159.                     correctInput = true;
  160.                 }
  161.             }
  162.  
  163.             if (!correctInput)
  164.             {
  165.                 System.out.println("Error!");
  166.             }
  167.         }
  168.     }
  169.  
  170. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement