0

I have this question.

I have the following method:

    public static Object[] czyDziala(String[] lista) throws IOException {

        for(int i=0 ; i<=lista.length-1;i++){
            URL url = new URL(lista[i]);
            HttpURLConnection httpCon = (HttpURLConnection) url.openConnection();

            int len = httpCon.getContentLength();
            if (len>0){

                System.out.println(url +new String(" Site Work"));

            }else{
                System.out.println(url +"Site Don't Work");

            }
        }
        return null;
    }

The argument in the method is a list of strings.

    JFrame okno = new JFrame();
    okno.setSize(1200, 500);
    okno.setVisible(true);
    okno.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    String [] col = {"Nazwa Witryny"};
    String [] listaWitryn = {"http://www.wp.pl",
                             "http://www.onet.pl","http://mobidev.pl"};


    try {
        Work.czyDziala(listaWitryn);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

How can I add a table that is

| Sites Name | Status |

I think that it is wrong method created? But I could be wrong.

Qantas 94 Heavy
  • 15,750
  • 31
  • 68
  • 83
user3278375
  • 63
  • 2
  • 7
  • 1
    Well currently your `czyDziala` method just returns `null`, always. You should probably change it to return a `boolean[]` indicating which sites "work" and which don't. Or change your code to a `checkSite` method taking a *single* site and returning a *single* value. – Jon Skeet Feb 06 '14 at 07:14
  • With the JTable class you can display tables of data: http://docs.oracle.com/javase/tutorial/uiswing/components/table.html – rebeliagamer Feb 06 '14 at 07:16
  • Could I write this code? – user3278375 Feb 06 '14 at 07:19
  • tabla is a different word entirely :) – SoulRayder Feb 06 '14 at 07:21

1 Answers1

0

Your code could be:

public static Map<String, Boolean> czyDziala(String[] lista) throws IOException {
    Map<String, Boolean> saveInDBList = new HashMap<String, Boolean>();
    for(int i=0 ; i<=lista.length-1;i++){
        URL url = new URL(lista[i]);
        HttpURLConnection httpCon = (HttpURLConnection) url.openConnection();

        int len = httpCon.getContentLength();
        if (len>0){

            System.out.println(url +new String(" Site Work"));
            saveInDBList.put(lista[i], true);
        }else{
            System.out.println(url +"Site Don't Work");
            saveInDBList.put(lista[i], false);

        }
    }
    return saveInDBList;
}

and

String [] col = {"Nazwa Witryny"};
    String [] listaWitryn = {"http://www.wp.pl",
                             "http://www.onet.pl","http://mobidev.pl"};


    try {
        Map<String, Boolean> saveInDBList = Work.czyDziala(listaWitryn);
        //Your Insert DB call
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}
Subbu
  • 308
  • 2
  • 4
  • 12
  • And how I display sites and status on the Jtable? – user3278375 Feb 06 '14 at 08:03
  • Pls refer this link 1) http://docs.oracle.com/javase/tutorial/uiswing/components/table.html#data 2) http://stackoverflow.com/questions/17212197/create-the-hashmap-based-on-jtable – Subbu Feb 06 '14 at 10:52