1

I have a text file with 4 different options "start time", "end time", "ID" and "value" each separated by a tab. i need to get each bit of information and set it equal to parameters in the constructor of my class. Iv done basic set and get methods before but never using data from a text file. Heres what iv got so far.

import java.util.Date;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

public class Sensor {
//need all info for sensors such as sensor ID, Ontime, Offtime, Label
    //needs to store all sensor data in memory
    //try using jfreechart again instead of manually drawing

    private Date startTime1;
    private Date endTime1;
    private String sensorID1;
    private int sensorState1;
    String fileName = "C:/kasterenDataset/kasterenSenseData.txt";

    public Sensor(Date startTime, Date endTime, String sensorID, int sensorState){
        startTime = startTime1;
        endTime = endTime1;
        sensorID = sensorID1;
        sensorState = sensorState1;


    }

    public void setStartTime(){
         String line = null;

            try {
                // FileReader reads text files in the default encoding.
                FileReader fileReader =  new FileReader(fileName);

                // Always wrap FileReader in BufferedReader.
                BufferedReader bufferedReader = new BufferedReader(fileReader);

                //start reading from the 24th line on the text file
                int lineNumber = 0;
                while((line=bufferedReader.readLine())!=null){
                    lineNumber++;
                    if (lineNumber >= 24) {
                            String values[] = line.split("\\t", -1);

                    }

                }
                bufferedReader.close();         
            }
            catch(FileNotFoundException ex) {
                System.out.println(
                    "Unable to open file '" + 
                    fileName + "'");                
            }

            catch(IOException ex1) {
                System.out.println(
                    "Error reading file '" 
                    + fileName + "'");                  
            }       
            }
    public void getStartTime(){

    }

    public void getEndTime(){


    }

    public void getSensorID(){

    }

    public void getSensorState(){

    }

}

This is some of the data from the text file

25-Feb-2008 23:22:31    25-Feb-2008 23:24:35    5   1
25-Feb-2008 23:28:31    25-Feb-2008 23:28:32    6   1
25-Feb-2008 23:28:36    25-Feb-2008 23:29:12    6   1
25-Feb-2008 23:30:12    25-Feb-2008 23:30:52    5   1
25-Feb-2008 23:31:53    25-Feb-2008 23:32:30    24  1
25-Feb-2008 23:32:33    25-Feb-2008 23:32:34    24  1
26-Feb-2008 00:38:49    26-Feb-2008 00:38:50    24  1
26-Feb-2008 00:38:52    26-Feb-2008 00:38:59    5   1
26-Feb-2008 00:38:53    26-Feb-2008 00:39:41    24  1
26-Feb-2008 00:39:00    26-Feb-2008 00:39:01    5   1
26-Feb-2008 00:39:03    26-Feb-2008 00:39:04    5   1
26-Feb-2008 00:39:09    26-Feb-2008 00:39:10    5   1
27-Feb-2008 23:23:57    27-Feb-2008 23:24:09    8   1
27-Feb-2008 23:26:19    27-Feb-2008 23:37:08    6   1
27-Feb-2008 23:37:01    27-Feb-2008 23:37:02    14  1
27-Feb-2008 23:37:05    27-Feb-2008 23:37:06    14  1
27-Feb-2008 23:37:09    28-Feb-2008 00:10:39    5   1
27-Feb-2008 23:42:20    27-Feb-2008 23:42:21    18  1
28-Feb-2008 00:10:42    28-Feb-2008 00:10:43    5   1
28-Feb-2008 00:11:10    28-Feb-2008 00:13:42    6   1
28-Feb-2008 00:13:38    28-Feb-2008 00:13:39    14  1
28-Feb-2008 00:13:42    28-Feb-2008 00:13:43    6   1
McCann46
  • 77
  • 1
  • 7

1 Answers1

1

Don't do like that. Getters and setters are just a convention used not to expose attributes directly. Create a method just to load data that is not a getter or setter.

Leo
  • 6,480
  • 4
  • 37
  • 52
  • This answer is correct. The convention comes from Sun's *JavaBeans* spec, as discussed in [this Question](http://stackoverflow.com/q/1991316/642706). Follow that link to find links to both the spec and implementation. – Basil Bourque Apr 03 '14 at 18:15