Input/Output for lines of text: Difference between revisions
(Created page with "{{draft task}} The first line contains the number of lines to follow, followed by that number of lines of text on STDIN. Output to STDOUT each line of input by passing it to a...") |
|||
Line 26: | Line 26: | ||
public static void main(String[] args) { |
public static void main(String[] args) { |
||
Scanner in = new Scanner(System.in); |
Scanner in = new Scanner(System.in); |
||
int n = Integer.parseInt(in.nextLine()); |
int n = Integer.parseInt(in.nextLine()); //doesn't use nextInt() so nextLine doesn't just read newline character |
||
for(int i=0; i<n; i++){ |
for(int i=0; i<n; i++){ |
||
String word = in.nextLine(); |
String word = in.nextLine(); |
||
Line 33: | Line 33: | ||
} |
} |
||
}</lang> |
}</lang> |
||
=={{header|Python}}== |
=={{header|Python}}== |
Revision as of 22:32, 8 January 2014
The first line contains the number of lines to follow, followed by that number of lines of text on STDIN. Output to STDOUT each line of input by passing it to a method as an intermediate step. The code should demonstrate these 3 things. See also Input/Output for Pairs of Numbers and File/Input and Output.
- Sample input with corresponding output
Input
3 hello hello world Pack my Box with 5 dozen liquor jugs
Output
hello hello world Pack my Box with 5 dozen liquor jugs
Java
<lang java>import java.util.Scanner;
public class Main { public static void doStuff(String word){ System.out.println(word); }
public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = Integer.parseInt(in.nextLine()); //doesn't use nextInt() so nextLine doesn't just read newline character for(int i=0; i<n; i++){ String word = in.nextLine(); doStuff(word); } } }</lang>
Python
<lang python>def do_stuff(word): print(word)
t = input() for x in range(0, t): word = raw_input() do_stuff(word)</lang>
Ruby
<lang ruby>def do_stuff(word) puts word end
t = gets.to_i for i in 1..t do word = gets do_stuff(word) end</lang>