Reputation: 5086
I have the following code:
BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
String submittedString = "";
System.out.flush();
submittedString = stdin.readLine();
int numberofLines = Integer.parseInt(submittedString.split(" ")[0]);
for(int i = 0; i < numberofLines; i++)
submittedString += stdin.readLine();
zipfpuzzle mySolver = new zipfpuzzle();
mySolver.getTopSongs(submittedString);
However, despite the input being over multiple lines, this only reads the first.
Where is my mistake?
If it makes any difference, I am compiling on eclipse.
Cheers! Dario
Upvotes: 0
Views: 9843
Reputation: 25950
Just use an array and populate it within your for-loop:
String[] inputs = new String[numberofLines];
for (int i = 0; i < numberofLines; i++)
inputs[i] = stdin.readLine();
Extra Note:
If you want multiple lines with single String:
String submittedString = "";
for (int i = 0; i < numberofLines; i++)
submittedString += stdin.readLine() + System.getProperty("line.separator");
Upvotes: 4
Reputation: 965
submittedString = stdin.readLine();
BufferedReaders readLine method will read System.in until it hits a new line, therefore if you're using the first line of the file to determine the number of lines to read then your data must be incorrect.
Upvotes: -1
Reputation: 12843
BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
String line = "";
while ((line = stdin.readLine()) != null){
// Do something.
submittedString += line + '\n';
}
Upvotes: 3