tags:

views:

119

answers:

4

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();
    }
}
+3  A: 

The while loop never stops

EDIT: Oops no, this is not true, but what is the point of having a while loop that runs once? You put a break at the end of it, so maybe you could refactor that to an if? Maybe a guard?

Alberto Zaccagni
+9  A: 
ArrayList num  = new ArrayList();

for(int i = 0; i < num.size(); i++)

means you go from 0 to... 0!

for(int i = 0; i < x; i++)

would be better.

VonC
+2  A: 

The size of num here is 0. You should use 'x' instead of num.size()

        for(int i = 0; i < num.size(); i++)
        {
            //String f = in.readLine();
            num.add(in.readLine());

        }
willcodejavaforfood
A: 

This might not be the exact what you have done.. a bit different.. :-)

int x = Integer.parseInt(in.readLine());   
 String[] arr = new String[x]; // Edited :: Slip of mind - Thanks Pgras    
 for(int i=0;i<x;i++){ //input x number of words and store..
arr[i] = in.readLine();
}

for(i=x-1;i>=0;i--){ //Display words in reverse order
System.out.println(arr[i]);
}
Richie
Why don't you write String[] arr = new String[x]; ???
pgras