I am trying to get my java code to write the counts from a GUI to a .txt file. I want the code to write date and time first and then on the same line, I want it to write the counts.
Something like this:
07/12/2018 11:20:56AM, 4500, 5000, 6500
07/12/2018 11:20:58AM, 4500, 5000, 6500
and so on.
But what I am getting is:
07/12/2018 11:20:56 AM,
4500,5000,6500
07/12/2018 11:20:56 AM,
4500,5000,6500
I do not know why there is a line break after the date and time stamp.
I have attached my code below:
What I have tried:
pollTimer.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
try {
if(r.read(POLL_FLAG_ADDRESS) != 0)
{ // if the data ready flag is up
if(tglbtnWrite.isSelected() && (intervalCounter%requiredParams[2] == 0))
printw.printf(dtf.format(LocalDateTime.now()) + ","); //For every new line of counts, shows current date and and time of the system.
r.write(POLL_FLAG_ADDRESS,0); // put it back down
intervalCounter++; // increment interval counter (gets reset by GO/STOP toggle)
for(int i = 0; i < MAX_WIDGETS; i++) { // loop through all widgets
NumWidget tmp = numwid.get(i);
if((tmp.getState() != 0) && (tmp.getState() != 16)) { // check if widget is configured for a channel
tmp.setAcc(tmp.getAcc() + r.read(translate[tmp.getState() - 1])); // if it is, poll the respective register
if(intervalCounter%requiredParams[2] == 0) { // check if we have reached integration interval
tmp.setTextFieldText(String.format("%,d", tmp.getAcc())); // if so, update display
if(tglbtnWrite.isSelected())
printw.printf(tmp.getAcc()+ ","); // if write enabled, write to disk
tmp.setAcc(0); // reset accumulator
}
}
// if this widget holds a user parameter and write is enabled, write parameter to file
if(tmp.getLabelPEnabled() && tglbtnWrite.isSelected()) printw.printf(tmp.getTextFieldText()+",");
}
if(tglbtnWrite.isSelected())
printw.printf("\r\n"); // if write enabled, end line/carriage return
if(intervalCounter == requiredParams[3]) {
pollTimer.stop(); // if we are at max data points, stop (always fails if data points > 0)
// reset GO button to stopped state
btnGO.setText("GO");
btnGO.setForeground(Color.GREEN);
btnGO.setSelected(false);
}
}
} catch (SerialPortException e) {
e.printStackTrace();
} catch (SerialPortTimeoutException e) {
e.printStackTrace();
}
}
});