Java by API/java.io/LineNumberReader — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
|
(нет различий)
|
Текущая версия на 14:32, 31 мая 2010
LineNumberReader: getLineNumber()
import java.io.InputStreamReader;
import java.io.LineNumberReader;
public class MainClass {
public static void main(String[] args) throws Exception {
LineNumberReader lineCounter = new LineNumberReader(new InputStreamReader(System.in));
String nextLine = null;
System.out.println("Type any text and press return. Type "exit" to quit the program.");
try {
while ((nextLine = lineCounter.readLine()).indexOf("exit") == -1) {
if (nextLine == null)
break;
System.out.print(lineCounter.getLineNumber());
System.out.print(": ");
System.out.println(nextLine);
}
} catch (Exception done) {
done.printStackTrace();
}
}
}
LineNumberReader: readLine()
import java.io.InputStreamReader;
import java.io.LineNumberReader;
public class MainClass {
public static void main(String[] args) throws Exception {
LineNumberReader lineCounter = new LineNumberReader(new InputStreamReader(System.in));
String nextLine = null;
System.out.println("Type any text and press return. Type "exit" to quit the program.");
try {
while ((nextLine = lineCounter.readLine()).indexOf("exit") == -1) {
if (nextLine == null)
break;
System.out.print(lineCounter.getLineNumber());
System.out.print(": ");
System.out.println(nextLine);
}
} catch (Exception done) {
done.printStackTrace();
}
}
}
new LineNumberReader(Reader in)
import java.io.InputStreamReader;
import java.io.LineNumberReader;
public class MainClass {
public static void main(String[] args) throws Exception {
LineNumberReader lineCounter = new LineNumberReader(new InputStreamReader(System.in));
String nextLine = null;
System.out.println("Type any text and press return. Type "exit" to quit the program.");
try {
while ((nextLine = lineCounter.readLine()).indexOf("exit") == -1) {
if (nextLine == null)
break;
System.out.print(lineCounter.getLineNumber());
System.out.print(": ");
System.out.println(nextLine);
}
} catch (Exception done) {
done.printStackTrace();
}
}
}