Speed up development with full-stack environments for every branch.

Learn More

Reading Strings from console in Java

637 Runs 11730 Views 3423 Copies
Saved

Saved

mirang 126

mirang
published 4 years ago

/*
The sample shows how you can reading Strings from console in java
*/
import java.io.*;
public class ConsoleReadingDemo {
  public static void main(String[] args) {
    String newLine = System.getProperty("line.separator");
    System.out.println("Reading Strings from console");
    
      //You use System.in to get the Strings entered in console by user
      try
      {
      //You need to create BufferedReader which has System.in to get user input
      BufferedReader br = new BufferedReader(new
                              InputStreamReader(System.in));
      String userInput;
      System.out.println("Enter text...");
      System.out.println("Enter 'quit' to quit.");
        do {
          //reader .readLine() would wait for the user to enter strings.
          //keep and entering text and when you press enter, all those would be displayed
          //so it is keeping it in buffer untill the user types 'quit' and presses enter
          userInput = (String) br.readLine();
          System.out.println("You entered : " + userInput);
        } while(!userInput.equals("quit"));
      }
      catch(Exception e)
      {
      }
  }
}


Please login/signup to get access to the terminal.

Your session has timed out.

Dismiss (the page may not function properly).