title: How to Compare a Substring to a Normal White Space in Java
body: <p>Sorry if this seems like a silly question, but I cannot figure out how to compare a substring from user input in Java to a white space. In my program, I use</p>
  
  <pre><code>      Input.getString()
  </code></pre>
  
  <p>which is not usual, but Input.class is used to receive input from a user. Here is my program:</p>
  
  <pre><code>public class Project1
  {
  static String E [] = { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "1", "2", "3", "4", "5", "6", "7", "8", "9", "0", " " };
  static String M [] = { ".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--..", ".----", "..---", "...--", "....-", ".....", "-....", "--...", "---..", "----.", "-----", "|" };
  
  public String input[];
  public String output[];
  static String TranslationType;
  static boolean Bool;
  static String TextToTranslate;
  static int i;
  static int SectionOfString;
  static StringBuffer emcStringBuffer = new StringBuffer();
  static StringBuffer mceStringBuffer = new StringBuffer();
  static String emc = "English-MorseCode";
  static String mce = "MorseCode-English";
  static StringBuffer MorseCodeLetter = new StringBuffer();
  static String emcString;
  static String mceString;
  static String space = " ";
  
  public static void main( String [] args )
  {
      if ( E.length != M.length )
      {
          System.out.println( "Error" );
      }
      TranslationType = Input.getString( "Please choose a method of translation (\"English-MorseCode\" or \"MorseCode-English\") " );
      if ( TranslationType.equals( "English-MorseCode" ) )
      {
          Bool = true;
          System.out.println( TranslationType );
      }
      else if( TranslationType.equals( "MorseCode-English" ) )
      {
          Bool = false;
          System.out.println( TranslationType );
      }
  
      while ( !TranslationType.equals( emc ) &amp;&amp; !TranslationType.equals( mce ) )
      {
          System.out.println( "Error: \"" + TranslationType + "\" is not a valid response.");
          TranslationType = Input.getString( "Please choose a method of translation (\"English-MorseCode\" or \"MorseCode-English\") " );
          if ( TranslationType.equals( "English-MorseCode" ) )
          {
              Bool = true;
              System.out.println( TranslationType );
          }
          else if( TranslationType.equals( "MorseCode-English" ) )
          {
              Bool = false;
              System.out.println( TranslationType );
          }
      }
      TextToTranslate = Input.getString( "What would you like to translate?" );
      TextToTranslate = TextToTranslate.toUpperCase();
      System.out.println( TextToTranslate );
      if ( Bool == true )
      {
          for ( SectionOfString = 0; SectionOfString &lt; TextToTranslate.length(); SectionOfString++ )
          {
              for ( i = 0; i &lt; E.length; i++ )
              {
                  if ( TextToTranslate.substring( SectionOfString, SectionOfString + 1 ).equals( E[i] ) )
                  {
                      emcStringBuffer.append( M[i] + " " );
                  }
              }
          }
          emcString = emcStringBuffer.toString();
          System.out.println( emcString );
      }
      else if ( Bool == false )
      {
          for ( SectionOfString = 0; SectionOfString &lt; TextToTranslate.length(); SectionOfString++ )
          {
              if ( TextToTranslate.substring( SectionOfString, SectionOfString + 1 ).equals( space ) )
              {
                  for ( i = 0; i &lt; M.length; i++ )
                  {
                      if ( MorseCodeLetter.equals( M[i] ) )
                      {
                          mceStringBuffer.append( E[i] );
                          System.out.println( MorseCodeLetter + ";" + mceStringBuffer );
                      }
                  }
              }
              else
              {
                  if ( TextToTranslate.substring( SectionOfString, SectionOfString + 1 ).equals( M[36] ) )
                  {
                      mceStringBuffer.append( E[36] );
                  }
                  else
                  {
                      MorseCodeLetter.append( TextToTranslate.substring( SectionOfString, SectionOfString + 1 ) );
                  }
              }
          }
          mceString = mceStringBuffer.toString();
          System.out.println( mceString );
      }
      System.out.println( "Done" );
  }
  }
  </code></pre>
  
  <p>The problem seems to be that this part of the code is never used while translating Morse Code into English:</p>
  
  <pre><code>if ( TextToTranslate.substring( SectionOfString, SectionOfString + 1 ).equals( space ) )
              {
                  for ( i = 0; i &lt; M.length; i++ )
                  {
                      if ( MorseCodeLetter.equals( M[i] ) )
                      {
                          mceStringBuffer.append( E[i] );
                          System.out.println( MorseCodeLetter + ";" + mceStringBuffer );
                      }
                  }
              }
  </code></pre>
  
  <p>This causes the morse code letters not to be separated. For example, if the input for TextToTranslate was</p>
  
  <pre><code>. .-. | .-. .
  </code></pre>
  
  <p>then the program would think that ..-..-. was a word. I'm not sure if this is the reason that the translated result does not print to the terminal ( or command prompt on windows ).</p>
  
  <p>Also, I tried to have space be a static char before, and I just tried having space as a final char, using the following in the if construct, and I had the same problem:</p>
  
  <pre><code>TextToTranslate.charAt( SectionOfString ) == space
  </code></pre>
  
  <p>I'm not sure why this problem happens, and I tried to find a similar question but was unable to, so if anyone knows how to fix the problem I would appreciate it.</p>
  
  <p>Note: I asked the same question on Java Programming Forums (user: Indybones33)</p>