Program to Read text entered by user in console till some special character (let's say @) in java








Program to Read text entered by user in console till some special character (let's say @) in java >




import java.io.DataInputStream;
import java.io.IOException;
/** JavaMadeSoEasy.com */
public class ReadFromUsersInput {
   public static void main(String...args) {
         
          //will wait for user to type some text and press enter
          DataInputStream dis = null;
         
          try {
                 System.out.println("Type some text and press enter...");
                 dis = new DataInputStream(System.in);
                 char ch;
                 while ((ch = (char) dis.read()) != '@') { //read till '@'
                       System.out.print(ch); // Display text typed by user in console
                 }
          } catch (IOException e) {
                 e.printStackTrace();
          }
         
          finally {
                 try {
                       if (dis != null)
                              dis.close(); //close DataInputStream
                 } catch (IOException e) {
                       e.printStackTrace();
                 }
          }
         
         
   }
}
/*OUTPUT
Type some text and press enter...
ankitJavamadesoEasy@com
ankitJavamadesoEasy
*/

In the above program user  entered ankitJavamadesoEasy@com in console but program read till ankitJavamadesoEasy only.

RELATED LINKS>

Program to Read text entered by user in console using DataInputStream in java file IO

Read text entered by use in consoler using BufferedReader's readLine() method in java file IO

Program to Read text entered by use in console using java.util.Scanner

Program to Create Directory - Single and multiple (i.e. parent and child directories) in java file IO



Program to Read text from file using FileInputStream and Try with resource provided in java 7


Write in file using FileOutputStream and BufferedOutputStream >

Write String to file using FileOutputStream in java file IO


No comments:

Post a Comment