2

I have this code

private void startActionPerformed(java.awt.event.ActionEvent evt) {                                      
    this.start.setBackground(Color.green);
    this.stop.setBackground(Color.lightGray);
    if (!stopped) {
        timer.cancel();
    }
    stopped = false;
    stato.setText("Avviato");
    timer = new Timer();
    if(giacRitardo>0)
        timer.schedule(S.run("argiacenze"), giacRitardo, giacRitardo);//parti dopo x secondi e itera ogni x secondi
    if(cliRitardo>0)
        timer.schedule(S.run("arclienti"), cliRitardo, cliRitardo);//parti dopo x secondi e itera ogni x secondi

// some other code

and

class TaskSchedulato extends TimerTask {

    @Override
    public void run() {
        redirectSystemStreams();
        DateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        URL sito = null;
        try {
            sito = new URL(sUrl + "?aggiornamento=arlingue");
        } catch (MalformedURLException ex) {
            System.out.println("Indirizzo del sito mal formato o inesistente");
        }
        URLConnection yc = null;
        try {
            yc = sito.openConnection();
        } catch (IOException ex) {
            System.out.println("Errore di connessione _ ");
        }
        BufferedReader in = null;
        try {
            in = new BufferedReader(
                    new InputStreamReader(
                            yc.getInputStream()));
        } catch (IOException ex) {
            Logger.getLogger(TaskSchedulato.class.getName()).log(Level.SEVERE, null, ex);
        }
        String inputLine;
        try {
            while ((inputLine = in.readLine()) != null) {
                System.out.println(inputLine);
            }
        } catch (IOException ex) {
            Logger.getLogger(TaskSchedulato.class.getName()).log(Level.SEVERE, null, ex);
            dataErrore = new Date();
            System.out.println(sdf.format(dataErrore));
            System.out.println("Errore di connessione: " + dataErrore);
        }
        try {
            in.close();
        } catch (IOException ex) {
            Logger.getLogger(TaskSchedulato.class.getName()).log(Level.SEVERE, null, ex);
        }

    }
}

and

TaskSchedulato S = new TaskSchedulato();

I need to extend the method run above, so that i can pass a string parameter to it. How can a do it. I'm almost a newbye in java. So, please forgive me for the inexperience.

Actually I receive the error: no suitable method found for run(String) method TimerTask.run() is not applicable (actual and formal argument lists differ in length) method MainForm.TaskSchedulato.run() is not applicable (actual and formal argument lists differ in length)

Thank you in advance

Nicola M
  • 49
  • 6
  • Possible duplicate of [How to Pass Arguments to Timertask Run Method](https://stackoverflow.com/questions/7512157/how-to-pass-arguments-to-timertask-run-method) – domsson Jul 04 '17 at 10:15

3 Answers3

3

This can be easily achieved with method overloading.

In short, method overloading is a language feature that allows to declare multiple methods with the same name, but different parameters.

Applied to your problem, instead of overriding the parent's run() method, just declare another run() method like this:

public void run(String someInput) {
    /* ... */
}

Of course, you can call run() from within run(String) if that makes sense in your program:

public void run(String someInput) {
    /* Do something with someInput */
    run(); // Hand over to parent's run() method
    /* Maybe do some other stuff */
}

Depending on what you are trying to do, you might want to use both, method overloading as well as overriding. Some more context would be required to give more specific advice.

domsson
  • 4,553
  • 2
  • 22
  • 40
  • Thank you domdom, – Nicola M Mar 31 '17 at 19:01
  • Thank you @domdom. I overloaded it. Now, when i call method as first parameter in timer.schedule, i receive a " 'void' type not allowed here" error. – Nicola M Mar 31 '17 at 19:06
  • Nicola, your `run()` method returns nothing (`void`). You are handing the return value of `run()` as first parameter to the `timer.schedule()` method. However, a method can not take a parameter of type `void`. What is the signature of `timer.schedule()`? What does it expect as first parameter? – domsson Mar 31 '17 at 21:36
1

I solved this way

class TaskSchedulato extends TimerTask {
    String stringa;
    public TaskSchedulato(String stringa){
        this.stringa=stringa;
    }
    @Override
    public void run() {
    //code here
    }

Thanks to @onurbaysan for the answer in the thread below How to Pass Arguments to Timertask Run Method

Community
  • 1
  • 1
Nicola M
  • 49
  • 6
0

@domdom, signature of timer.schedule is schedule(TimerTask task,long delay,long period)

I already used it in my working version of the program

private void startActionPerformed(java.awt.event.ActionEvent evt) {                                      
    this.start.setBackground(Color.green);
    this.stop.setBackground(Color.lightGray);
    if (!stopped) {
        timer.cancel();
    }
    stopped = false;
    stato.setText("Avviato");
    timer = new Timer();
    if(giacRitardo>0)
        timer.schedule(new TaskGiacenze(), giacRitardo, giacRitardo);
    if(cliRitardo>0)
        timer.schedule(new TaskClienti(), cliRitardo, cliRitardo);
    if(artRitardo>0)
        timer.schedule(new TaskArticoli(), artRitardo, artRitardo
    //..........

and

class TaskGiacenze extends TimerTask {

    @Override
    public void run() {
        redirectSystemStreams();
        DateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        URL sito = null;
        try {
            sito = new URL(sUrl + "?aggiornamento=argiacenze");
        } catch (MalformedURLException ex) {
            System.out.println("Indirizzo del sito mal formato o inesistente");
        }
        URLConnection yc = null;
        try {
            yc = sito.openConnection();
        } catch (IOException ex) {
            System.out.println("Errore di connessione _ ");
        }
        BufferedReader in = null;
        try {
            in = new BufferedReader(
                    new InputStreamReader(
                            yc.getInputStream()));
        } catch (IOException ex) {
            Logger.getLogger(TaskGiacenze.class.getName()).log(Level.SEVERE, null, ex);
        }
        String inputLine;
        try {
            while ((inputLine = in.readLine()) != null) {
                System.out.println(inputLine);
            }
        } catch (IOException ex) {
            Logger.getLogger(TaskGiacenze.class.getName()).log(Level.SEVERE, null, ex);
            dataErrore = new Date();
            System.out.println(sdf.format(dataErrore));
            System.out.println("Errore di connessione: " + dataErrore);
        }
        try {
            in.close();
        } catch (IOException ex) {
            Logger.getLogger(TaskGiacenze.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}

but I had to create a class for each kind (one class for giacenze, another class for articoli etc.. etc...) instead of calling it as parameter.

Nicola M
  • 49
  • 6