Java Programming Example of how to remove the extra spaces from the given string?

This program accepts the string from the keyboard and removes the extra spaces from both side of the given string.

import java.io.*;

/**
 *
 * @author JavaHotSpot
 */

public class removeExtraSpaces
{
    public static void main(String args[])
 {
     String s1,s2;
     int length;
     DataInputStream d;
     try
     {
     System.out.println("Enter the String");
     d=new DataInputStream(System.in);
     s1=(String)d.readLine();
     s2=s1.trim();
     System.out.println("The Given String  = "+s1);
     System.out.println("String after removing extra spaces= "+s2);
     }
     catch(Exception e)
     {
         System.out.println(e.toString());
     }
 }
}

Output:
Enter the String
      JavaHotSpot
The Given String  =       JavaHotSpot
      String after removing extra spaces= JavaHotSpot