Joe Parks
Posts: 107
Nickname: joeparks
Registered: Aug, 2003
|
|
Re: Counter Newbie Help
|
Posted: Aug 13, 2003 3:39 PM
|
|
Be careful of an infinite loop. The data read in via readLine() will never include the newline character.
This will do what you want:
package com.parksindustries.learning.linereader;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
/**
* This simple application will show that the readLine()
* method of a Reader (in this case, a BufferedReader) chomps
* off the newline character(s).
*
* @author Joe Parks
* @version 1
* Date: Aug 13, 2003
* Time: 5:23:32 PM
*/
public class Application {
/**
* Read from standard in using the readLine() method.
* Iterate over the characters of the resultant string.
* Note that there is no blank line between the last character
* and the border ("<<<<<<>>>>>>").
* @param args
*/
public static void main(String[] args) {
InputStreamReader inputStreamReader;
inputStreamReader = new InputStreamReader(System.in);
BufferedReader input;
input = new BufferedReader(inputStreamReader);
try {
String line = input.readLine();
for (int i = 0; i < line.length(); i++) {
char c = line.charAt(i);
System.out.println("c = " + c);
}
System.out.println("<<<<<<>>>>>>");
} catch (IOException e) {
System.err.println("LOOK OUT!");
e.printStackTrace();
}
}
}
|
|