/home/wpollock1/public_html/restricted/Java1/TrailingSpace.java

// Model solution to this Java task:
// 1) Read a line of input from a user that may contain leading
//    and/or trailing space.
// 2) Create a new String by deleting trailing space, but not leading space.
//    (String.trim() removes both; there's no built-in method for this.)
// 3) If the input string only contains space, you can treat it all as either
//    leading OR trailing (but state which in a comment).
//
//   The method below treats input of all space as all trailing space.
//
// Written 9/2008 by Wayne Pollock, Tampa Florida USA

import javax.swing.JOptionPane;

class TrailingSpace {
   public static void main ( String [] args ) {
      String input = JOptionPane.showInputDialog( "Enter a line of text:" );
      if ( input == null )  return;

      // The easy way (using a regular expression):
      // This replaces a run of zero or more trailing spaces with nothing.
      String result = input.replaceFirst( "\\s*$", "" );

/*
      // The ugly way:
      String result = input;
      while ( result.endsWith( " " ) )
         result = input.substring( 0, result.length() - 1 );
*/

      System.out.println( "The input was \"" + input + "\"" );
      System.out.println( "The result is \"" + result + "\"" );
   }
}