What I'm trying to do is I have this program which reads in a number, and that number designates how many words there are, for example:
3
red
blue
green
And then prints out that same text but in reverse order so it would be
green
blue
red
followed by a blank line indicating to the server that you are done with that specific problem. But I seem to have a bug in my code somewhere.
I tried to store the words in an array List. I used a for
loop to store them in the list and then to print them out in reverse order I just used another for loop going the opposite way, starting from the end of the list going to the beginning.
When I run the program from the command prompt it just goes to the next command prompt line as if I had as it to compile the program, there are no errors but when I did a test, using a test program I created, it seems that the program reads the number but then goes and prints out a blank array.
It would seem as though the words from the server don't get stored in the array and I'm not sure what I'm doing wrong. I'm not the greatest programmer so any help would greatly be appreciated.
The Code:
import java.io.*;
import java.util.*;
public class Solution
{
public static void run(BufferedReader in, PrintWriter out)
throws IOException
{
int x = Integer.parseInt(in.readLine());
while(x != 0)
{
ArrayList num = new ArrayList();
for(int i = 0; i < num.size(); i++)
{
//String f = in.readLine();
num.add(in.readLine());
}
//System.out.println(num);
for(int i = num.size()-1; i > 0; i--)
{
out.println(num.get(i));
//x = Integer.parseInt(in.readLine());
System.out.println();
}
break;
}
out.flush();
}
}