public class Solution {
public static void main(String[] args) throws IOException {
BufferedReader buff = new BufferedReader(new InputStreamReader(System.in));
//write your code here
int max = Integer.MAX_VALUE;
ArrayList<String> list = new ArrayList<> ();
for (int x = 0; x < max; x++) {
list.add(buff.readLine());
if (list.get(x).equals("end")) {
list.remove("end");
break;
}
}
for (int x = 0; x < max; x++) {
System.out.println(list.get(x));
}
}
}
INTPUT ==
Crusader
Paladin
Warrior
Archer
Sharpshooter
Angel
Demon
end
OUTPUT ==
Crusader
Paladin
Warrior
Archer
Sharpshooter
Angel
Demon
Correct output, but not satisfying conditions
Resolved
Comments (7)
- Popular
- New
- Old
You must be signed in to leave a comment
Guadalupe Gagnon
29 November 2019, 15:34
You need to attach solution or we are not able to help. With the task making specific requirements we have no idea why code will fail verification.
0
Kris
29 November 2019, 15:46
Well ok,
Conditions ==
The end
Create a list of strings.
Enter strings from the keyboard and add them to the list.
Enter strings from the keyboard until the user enters "end". The string "end" is ignored.
Display the strings on the screen, each on a new line.
Requirements:
1. Declare a string list variable and immediately initialize it.
2. Read strings from the keyboard and add them to a list until the user enters "end".
3. Do not add "end" to the list.
4. Display the list, each value on a new line.
5. Use a for loop.
Accepted Solution ==
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 buff = new BufferedReader(new InputStreamReader(System.in));
//write your code here
// int max = Integer.MAX_VALUE;
ArrayList<String> list = new ArrayList<> ();
for (int x = 0; x < 5000; x++) {
list.add(buff.readLine());
if (list.get(x).equals("end")) {
list.remove("end");
break;
}
}
for (int x = 0; x < list.size(); x++) {
System.out.println(list.get(x));
}
}
}
0
Guadalupe Gagnon
29 November 2019, 15:51useful
for (int x = 0; x < 5000; x++) {
this line should be:
while (true){
That makes an infinite loop, which inside you have a solution to break it when the user enters "end". My only suggestion would be to do this instead:
+1
Kris
29 November 2019, 16:04
Thanks a lot I saw the solution with the while loop also somewhere else, but just wondered why my code was accepted with the changes I made and it was not working with MAX_VALUE.
For clarification =
This 3 lines were not accepted =
int max = Integer.MAX_VALUE;
for (int x = 0; x < max; x++) {
for (int x = 0; x < max; x++) {
Changing them to
// int max = Integer.MAX_VALUE;
for (int x = 0; x < 5000; x++) {
for (int x = 0; x < list.size(); x++) {
satisfied all conditions. although the Output in both codes was the same, so it might be just a conditions thing as both codes seemed legit. At least I could not find another issue.
Btw thanks for being pro-active and helping us newbies with the tasks. We appreciate it.
Kris
0
Guadalupe Gagnon
29 November 2019, 16:30solution
The last for loop definitely needs to run with list.size() as the requirement. If you used another number, the likely hood of it actually being the size of the list is very small. Lets say, for example, you use max like you did. If the size of the list is anything less than max, then once the loop reaches an index higher than the amount of elements contained within, an IndexOutOfBoundsException will be thrown when list.get(x) is reached, and (if left unhandled) will crash the program. Also, if the number you use is smaller than the amount of elements in list then the program won't output all the contained data.
+2
Guadalupe Gagnon
29 November 2019, 16:31
And its my pleasure to help. I still learn a lot from helping others, especially practice using the debugging tools in IntelliJ.
+1
Kris
29 November 2019, 15:11
Btw by not using MAX_VALUE and changing the first max to 5000 and the second max to list.size() satisfied the conditions, although the output was also correct with MAX_VALUE
0