-1

I'm learning about servlets in java and I'm trying to capture a URL content and store it into a string array. Bellow is my code that I put together following some tutorials that does not seem to work servlet environment (This is the first part of an exercise that I'm trying to do).

I'm getting the error at this line:

cookies.add(line);

Here is the complete code:

//package fortune;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.ArrayList;
import java.util.Random;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import static java.util.Arrays.*;

@WebServlet(name = "FortuneServlet", urlPatterns = {"/"})
public class FortuneServlet extends HttpServlet {
    private String [] cookies = null;
    //ArrayList<String[]> cookies = new ArrayList<String[]>();

    public void geturl(String[] args) {

        try 
        {
         URL url = new URL(" http://fortunes.cat-v.org/openbsd/");
         // read text returned by server
             BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
         String line = in.readLine();

         while((line = in.readLine()) != null)
         {
              cookies.add(line);
              line = in.readLine();
         }
            in.close();

    }
        catch (java.net.MalformedURLException e) {
            System.out.println("Malformed URL: " + e.getMessage());
        }
        catch (IOException e) {
            System.out.println("I/O Error: " + e.getMessage());
        }
    }
    public void init() throws ServletException {
        // Add your own code here!
        // 1) Open the URL
        // 2) Get the contents
        // 3) Extract the cookie texts to an array


    }

    @Override
    protected void doGet(
        HttpServletRequest request, 
        HttpServletResponse response)
            throws ServletException, IOException {

        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/plain");
        if (cookies != null) {
            response.getWriter().println(
                cookies[new Random().nextInt(cookies.length)]

            );
        } else {
            response.getWriter().println("No luck!");
        }
    }
}
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
zamzam
  • 347
  • 1
  • 2
  • 9

2 Answers2

3
private String [] cookies = null;

Here you initialize an array of strings and set it to null.

cookies.add(line);

Here you assume that there is an add() method for the array, which it doesn't have.

You should go with the commented out solution, using a List, which has the add() method. You shouldn't use a list of arrays, but a list of strings:

List<String> cookies = new ArrayList<>();

Then you can add the cookies in the way you already do:

cookies.add(line);
Magnilex
  • 11,584
  • 9
  • 62
  • 84
  • thanks for the response now I'm getting an error saying "array required but list found " at this line: cookies[new Random().nextInt(cookies.length)] – zamzam Oct 02 '15 at 09:16
0

You cant add() method on an array. You have to use List. So instead of

private String [] cookies = null;

use

private List<String> cookies = new ArrayList<String>();
Rahman
  • 3,755
  • 3
  • 26
  • 43