How to read a string from console using readLine() in Java

1 Answer

0 votes
package javaapplication1;

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class JavaApplication1 {
 
    public static void main(String[] args) {
 
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
                 
        String s = null;
                 
        System.out.println("Write a Line of Text, '.' to quit");
                 
        try
        {
            while ( (s = br.readLine()) != null)
            {
                    if (s.equals("."))
                        break;
                                       
                    System.out.print("The Line of Text is: "  + s);
                    System.out.println();
            }
                       
            br.close();                    
        }
        catch(Exception e)
        {
              System.out.println(e.getMessage());
        }
     }
}
 
 
/*
 
run:
                    
Write a Line of Text, '.' to quit
java programming
The Line of Text is: java programming
windows
The Line of Text is: windows
android
The Line of Text is: android
web
The Line of Text is: web
.
  
*/

 



answered Mar 17, 2017 by avibootz
edited Mar 17, 2017 by avibootz

Related questions

1 answer 86 views
1 answer 133 views
1 answer 170 views
1 answer 174 views
1 answer 180 views
1 answer 153 views
153 views asked Oct 12, 2022 by avibootz
...