2016-04-03 14 views
0

私は私が手にこのプログラムのコードを実行します。JAVAエンド複数行入力

import java.util.Scanner; 

public class Capital { 
    public static void main(String []args) { 

     Scanner kbd = new Scanner(System.in); 

     while (kbd.hasNextLine()) { 
     String str = kbd.nextLine(); 

     System.out.println(str.toUpperCase()); 

     } 
    } 
} 

各入力に対する出力、例えば

私はプログラムを設定するにはどうすればよい
input: abc 
output:ABC 
input: xyz 
output:XYZ 

ファイルの終わりを宣言する前に複数の行を入力できるようにするには? like:

input: abc 
     xyz 
     aaa 
     ...etc 

output: ABC 
     XYZ 
     AAA 
     ...etc 

私が気付いたら、私は気付いてしまいます。

ご協力いただきありがとうございます。

+0

あなたはファイルからの入力を取っていません。それではEOFに達したかどうかをどうやって確認できますか? – Rehman

+0

最後の入力としてCtrl-Zを試してください – Turo

答えて

0

最後に出力したいので、入力をどこかに保存することをおすすめします。リストを作成し、入力の終わりに達するとそれらを出力するだけです。

Scanner kbd = new Scanner(System.in); 

List<String> input = new ArrayList<>(); 
while (kbd.hasNextLine()) 
    input.add(kbd.nextLine()); 

// after all the input, output the results. 
for (String str : input) 
    System.out.println(str.toUpperCase()); 
+1

ありがとうございます!!!!!! –

0
import java.io.BufferedReader; 
import java.io.IOException; 
import java.io.InputStreamReader; 

public class EndOfFile { 
public static void main(String[] args) throws IOException { 
    BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); 
    int n = 1; 
    String line; 
    while ((line=br.readLine())!=null) { 
     System.out.println(n + " " + line); 
     n++; 
    } 

    } 
} 
関連する問題