Click here to Skip to main content
15,889,604 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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();
				}
			}
		});
Posted
Comments
Richard MacCutchan 12-Jul-18 11:43am    
What control characters do you have in dtf.format .
Member 13909214 12-Jul-18 13:42pm    
What does that mean?
Richard MacCutchan 13-Jul-18 1:40am    
Sorry, my mistake, I mistook it for a print format string. However, it seems likely that the string created by dtf.format contains a terminating new line character. You can easily check it with your debugger.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900