title: String Output Alignment in Java
body: <p><a href="http://i.stack.imgur.com/jq7Cl.png" rel="nofollow">practice question part 1</a></p>
  
  <p><a href="http://i.stack.imgur.com/XNXgG.png" rel="nofollow">practice question part 2</a></p>
  
  <p>This is a practice question which is rather hard for me. Below is my code for the static method(the main method is fixed -unchangeable, and signature of static method is given), and my intention is to get the matches between the characters and print them out. </p>
  
  <p>But there are some concerns: </p>
  
  <p>1) How do i ensure it doesn't print when all the strings are aligned but there are extra characters which makes the boolean false and the result to be not aligned instead? (e.g amgk as second string &amp; first string is Java Programming Course) </p>
  
  <p>2) How do i make it print right? currently the spaces are off and the letters aren't what is wanted. </p>
  
  <p>3) If there is more than one character a in str1, which do i choose to put, and how do i omit the rest when there is already a match?</p>
  
  <p>Would really appreciate a pseudocode to help guide a beginner like me in solving this problem.</p>
  
  <pre><code>public class Q3 {
  public static void main(String[] args) {
      Scanner sc = new Scanner(System.in);
      System.out.print("Enter the first string:");
      String input1 = sc.nextLine();
      System.out.print("Enter the second string:");
      String input2 = sc.nextLine();
      System.out.println();
  
      if (matchStrings(input1, input2)) {
          System.out.println();
          System.out.println("There is an alignment as shown above.");
      } else {
          System.out.println("No alignment can be found.");
      }
  }
      public static boolean matchStrings(String str1, String str2) {
      // Modify the code below to return the correct value.
      boolean isMatch = false;
      //int firstChar = str2.charAt(0);
      //int lastChar = str2.charAt(str2.length()-1);
      int prevIndex = 0;
  
      System.out.println(str1);
  
      for (int j = 0; j&lt; str2.length(); j++) {
          for (int i = 0; i&lt;str1.length();i++) {
              char charToSearch = str1.charAt(i);
              int newIndex = i;
  
              if (str2.charAt(j)== charToSearch) {
                  for (int k = prevIndex; k &lt; newIndex-1; k++) {
                      System.out.print(" ");
                  }
                  System.out.print(charToSearch);
                  //prevIndex=newIndex+1;
  
  
                  isMatch = true;
              }
          }
      }
      return isMatch;
  }
  }
  </code></pre>