Here is a Java Program for Using trim() to process commands.
Output of Above Java Program
Enter 'stop' to quit.
Enter State:
California
Capital is Sacramento.
stop
import java.io.*; class UseTrim { public static void main(String args[]) throws IOException { // create a BufferedReader using System.in BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String str; System.out.println("Enter 'stop' to quit."); System.out.println("Enter State: "); do { str = br.readLine(); str = str.trim(); // remove whitespace if(str.equals("Illinois")) System.out.println("Capital is Springfield."); else if(str.equals("Missouri")) System.out.println("Capital is Jefferson City."); else if(str.equals("California")) System.out.println("Capital is Sacramento."); else if(str.equals("Washington")) System.out.println("Capital is Olympia."); // ... } while(!str.equals("stop")); } }
Output of Above Java Program
Enter 'stop' to quit.
Enter State:
California
Capital is Sacramento.
stop