Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package substringfinder;
- import java.util.Scanner;
- /**
- *
- * @author jussi
- */
- public class SubstringFinder
- {
- /**
- * @param args the command line arguments
- */
- static String input = "";
- static String subString = "";
- static char jokerPosition = ' ';
- public static void main(String[] args)
- {
- Scanner reader = new Scanner(System.in);
- boolean running = true;
- System.out.println("Hello! I find substrings.");
- while (running)
- {
- // go through the reader until correct input is taken
- getCorrectInput(reader);
- int iterator = 0;
- int length = subString.length();
- // if the joker position is a the start of subString iterate the loop to one
- if (jokerPosition == 's')
- {
- iterator = 1;
- }
- // if the joker position is at the end of subString shorten the loop by one
- if (jokerPosition == 'e')
- {
- length = subString.length() - 1;
- }
- String output = "";
- for (int i = 0; i < input.length(); i++)
- {
- // make dummy output which we can rely on
- String iterationOutput = output;
- // make dummy iterator in which we can rely on
- int iteratorCheck = i;
- // if the characters match go into loop
- if (input.charAt(i) == subString.charAt(iterator))
- {
- boolean found = false;
- // if we have joker position currently or there would be theoritically possibility of being a match go into secondary loop
- if (((i == 0 && jokerPosition == 's') || (i == input.length() - length && jokerPosition == 'e') || jokerPosition == ' ') && i <= input.length() - length)
- {
- // here we iterate both of the loops at the same time
- for (int j = iterator; j < length; j++, i++)
- {
- // if we find a match add the letter to the output
- if (input.charAt(i) == subString.charAt(j))
- {
- found = true;
- output += input.charAt(i);
- } else
- //otherwise break out of the loop
- {
- found = false;
- break;
- }
- }
- //if we find a match we print it out
- if (found)
- {
- //iterate the i with empty spaces until it's the lenght of the original input
- while (i <= input.length() - 1)
- {
- output += "-";
- i++;
- }
- System.out.println(output);
- }
- // now we reset the output to the original state of the current loop
- output = iterationOutput;
- }
- }
- output += "-";
- // and now we reset the iterator to original state of the current loop
- i = iteratorCheck;
- }
- boolean continueCheck = false;
- String choice = "";
- // check if the user wants to continue
- while (!continueCheck)
- {
- System.out.println("Continue (y/n)?");
- choice = reader.nextLine();
- switch (choice)
- {
- case "y":
- continueCheck = true;
- break;
- case "n":
- continueCheck = true;
- running = false;
- System.out.println("See you soon.");
- break;
- default:
- System.out.println("Error!");
- break;
- }
- }
- }
- }
- //basically just checking if the input is correct and if there is joker (*) in place
- public static void getCorrectInput(Scanner aReader)
- {
- Scanner reader = aReader;
- boolean correctInput = false;
- while (!correctInput)
- {
- input = "";
- subString = "";
- jokerPosition = ' ';
- System.out.println("Please, enter a string:");
- input = reader.nextLine();
- System.out.println("Please, enter a substring");
- subString = reader.nextLine();
- int inputLength = input.length();
- int subStringLength = subString.length();
- if (inputLength > 0 && subStringLength > 0 && !(subString.equals("*")))
- {
- if (subString.charAt(0) == '*' || subString.charAt(subStringLength - 1) == '*')
- {
- subStringLength--;
- if (subString.charAt(0) == '*')
- {
- jokerPosition = 's';
- } else
- {
- jokerPosition = 'e';
- }
- }
- if (subStringLength < inputLength)
- {
- correctInput = true;
- }
- }
- if (!correctInput)
- {
- System.out.println("Error!");
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement