Java String trim remove space from console input string

Description

Java String trim remove space from console input string


// Using trim() to process commands.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {
  public static void main(String args[]) throws IOException {
    // create a BufferedReader using System.in
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    String str;//from w w  w.j  a  v  a2  s .  c  o  m

    System.out.println("Enter 'stop' to quit.");
    System.out.println("Enter State: ");
    do {
      str = br.readLine();
      str = str.trim(); // remove whitespace

      if (str.equals("dir"))
        System.out.println("dir");
      else if (str.equals("cd"))
        System.out.println("cd");
      else if (str.equals("rd"))
        System.out.println("rd");
      else if (str.equals("ls"))
        System.out.println("");
    } while (!str.equals("stop"));
  }
}



PreviousNext

Related