Personal tools
You are here: Home documents lectures Java Sample Programs コンソールから文字列を読み込む
Document Actions

コンソールから文字列を読み込む

by member last modified 2007-03-19 15:53

実行するとプロンプト”>”をコンソールに出力します。 キーボードから文字列を入力します。 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) );
}
}

Powered by Plone, the Open Source Content Management System

This site conforms to the following standards: