コンソールから文字列を読み込む
実行するとプロンプト”>”をコンソールに出力します。 キーボードから文字列を入力します。 Qを入力すると終了し、それまでに入力した行をすべて書き出します。
import java.util.Vector;
import java.io.*;
public class Foo {
public Vector<String> readFromKeyboard()
{
Vector<String> V = new Vector<String>();
try {
BufferedReader br = new BufferedReader
(new InputStreamReader(System.in));
String line =null;
while (true)
{ /* Loop until input is 'Q' */
System.out.print(">");
line = br.readLine();
/* Exit from the loop */
if ((line == null) || line.equals("Q"))
break;
/* Push input data into V */
V.add(line);
}
}
catch (Exception e)
{ e.printStackTrace(); }
return V;
}
public static void main(String[] args)
{
Foo F = new Foo();
Vector<String> V = F.readFromKeyboard();
/* Output all elements in V */
for(int i=0; i < V.size(); i++)
System.out.println( V.get(i) );
}
}