Ben
Ben

Reputation: 55

Read .csv files simultaneously

I need to pull data from 4 separate .csv data files at timed intervals to output the numbers simultaneously. I have only been able to get the files to pull sequentially. I am not sure if threads will help and I have never been able to get them to run correctly. My end result will be in a JFrame to display the output.

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Scanner;


public class Inputtest {
    public static void main(String[] args) throws IOException,     InterruptedException {

        O21 a = new O21();
        O22 b = new O22();
        PH1 c = new PH1();
        PH2 d = new PH2();

        a.O21();
        b.O22();
        c.PH1();
        d.PH2();
    }
}

class O21
{
    public void O21() throws FileNotFoundException, InterruptedException {
        int e = 21;         //preset Level

        Scanner O1 = new Scanner(new File("O21.txt"));
        O1.useDelimiter(",");
        while (O1.hasNext()) {
            String a = O1.next();
            int aa = Integer.parseInt(a);
            Thread.sleep(500);      // Time delay for output

            if (a.trim().isEmpty()) {        continue;      }
            if(aa > (e + 1)) {
                System.out.println(a);
                System.out.println("Alarm high");
                continue;
            }
            if(aa < (e-1)) {
                System.out.println(a);
                System.out.println("Alarm Low");
                continue;
            }
            System.out.println(a);
        }
    }
}

class O22 {
    public void O22() throws FileNotFoundException, InterruptedException {
        double f = 20;          //preset Level
        Scanner O2 = new Scanner(new File("O22.txt"));
        O2.useDelimiter(",");
        while (O2.hasNext()) {
            String b = O2.next();
            int bb = Integer.parseInt(b);
            Thread.sleep(500);      // Time delay for output

            if (b.trim().isEmpty()) {        continue;      }

            if(bb > (f + 1)) {
                    System.out.println(b);
                    System.out.println("Alarm high");
                    continue;
            }

            if (bb < (f - 1)) {
                System.out.println(b);
                System.out.println("Alarm Low");
                continue;
            }

            System.out.println(b);
        }
    }
}

class PH1 {
    public void PH1() throws FileNotFoundException, InterruptedException {
        double g = 7;           //preset Level

        Scanner P1 = new Scanner(new File("PH1.txt"));
        P1.useDelimiter(",");
        while (P1.hasNext()) {
            String c = P1.next();
            double cc = Double.parseDouble(c);
            Thread.sleep(1000);     // Time delay for output

            if (c.trim().isEmpty()) {        continue;      }
            if (cc > (g + .5)) {
                System.out.println(c);
                System.out.println("Alarm high");
                continue;
            }

            if (cc < (g - .5)) {
                System.out.println(c);  
                System.out.println("Alarm Low");
                continue;
            }
            System.out.println(c);
        }
    }
}

class PH2 {
    public void PH2() throws FileNotFoundException, InterruptedException {
        double h = 7;           //preset Level

        Scanner P2 = new Scanner(new File("PH2.txt"));
        P2.useDelimiter(",");
        while (P2.hasNext()) {
            String d = P2.next();
            double dd = Double.parseDouble(d);
            Thread.sleep(1000);     // Time delay for output
            if (d.trim().isEmpty()) {        continue;      }
            if (dd > (h + .5)) {
                System.out.println(d);
                System.out.println("Alarm high");
                continue;
            }
            if (dd < (h - .5)) {
                System.out.println(d);
                System.out.println("Alarm Low");
                continue;
            }

            System.out.println(d);
        }
    }
}

Upvotes: 2

Views: 106

Answers (1)

Anders R. Bystrup
Anders R. Bystrup

Reputation: 16050

Yes, threading will help you. Very simply,

class O21 implements Runnable
{
    @Override public void run()  // Used to be your constructor
    {
        ....

And in your main method:

...
Thread a = new Thread( new O21() );
a.start();
...

and similarly for O22, PH1 and PH2 which will now all execute concurrently.

You can add a constructor to your Runnables to pass along refs to your JFrame. I'll leave that as an exercise. Also, there are plenty of resources on thread programming here on SO and elsewhere.

Cheers,

Upvotes: 1

Related Questions