package com.codegym.task.task07.task0722;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
/*
The end
*/
public class Solution {
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
ArrayList<String> list = new ArrayList<String>();
while(true)
{
String br = reader.readLine();
if(br.eqauls("the end"))
{
break;
}
else
{
list.add(br);
}
}
for(int i=0;i<list.size();i++)
{
System.out.println(list.get(i));
}
}
}
Can someone help?
Under discussion
Comments (6)
- Popular
- New
- Old
You must be signed in to leave a comment
Joy Majumdar
5 February 2019, 16:42
You are right but you can do this code also. Saves a bit memory
while (true)
{
list.add(reader.readLine());
if (list.contains("end"))
{
list.remove(list.size() - 1);
break;
}
}
0
Guadalupe Gagnon
5 February 2019, 18:19
I don't think this is the case. I created a program that reads in a text file and then runs the code using:
and then with your code of checking the list for end each time. The text file had 10681 words in it. When I timed the above code it input all the values in 4 ms. When I changed the code to your example and then ran it, it ran in 338ms. This is quite an increase in computer resources used. The reason is that list.contains(object) checks every item in the list for a match. The large the list gets the slower and slower the code works. 0
satya vath
26 November 2018, 12:14
0
Ankush Rajput
27 September 2018, 12:57
Its not "the end", only "end". Read ques carefully.
0
Yusra Ansari
27 September 2018, 16:13
Still It gives me error when I wrote if(br.eqauls("end"))
Its not even giving output
0
Ankush Rajput
27 September 2018, 16:35useful
check if spelling of "equals" is correct in your program. Its wrong in the comment you posted.
Rest of the program is fine.
+1