How to read a line of characters (sentence) from console using BufferedReader() in Java

1 Answer

0 votes
package javaapplication1;

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

public class JavaApplication1 {

    public static void main(String[] args) throws IOException {

        try (BufferedReader br = new BufferedReader(new InputStreamReader(System.in))) {

            String s = null;

            System.out.print("Type a sentence (type end to exit): ");

            while ((s = br.readLine()) != null) {
                if (s.equals("end")) {
                    break;
                }
                System.out.println("The sentence: " + s);
            }

        } catch (Exception e) {
            System.out.print(e.toString());
        }
    }
}

/*
           
run:
     
Type a sentence (type end to exit): Java is a general-purpose computer programming language
The sentence: Java is a general-purpose computer programming language
end
  
 */

 



answered Nov 22, 2016 by avibootz

Related questions

1 answer 157 views
1 answer 183 views
1 answer 162 views
1 answer 109 views
...