Home » Archive

Articles tagged with: java Using BufferedReader

Java Codes »

[13 Feb 2009 | One Comment | 1,500 views]

This code example takes input from user and write the contents of the input to a file for every time the user hits the enter key.
This is continued until the user types the word ‘finished’, then the program exists. A StringBuilder is used to check whether the word ‘finished’ has been typed in.

package com.javadb.examples;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;

Java Codes »

[13 Feb 2009 | One Comment | 1,795 views]

This example shows how to read input from console using the BufferedReader class. The BufferedReader has a neat method called readLine() that reads all the input characters into a string when the user hits the Enter button instead of reading byte by byte.
This example takes the input string and parses it into a variable of type Double and then prints out the square root of that number.
This is the code:
package com.javadb.examples;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
/**
 *
 * @author www.javabout.com
 */
public class Main {
    public static void main(String[] args) {
        try {
            
            BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
            String input …