java - Java中同一行的日期/时间和数据计数
问题描述
我正在尝试编辑我的代码以提供当前日期和时间,然后在同一行上计算一些数据。我附上了下面的java代码。
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())
//s.append(tmp.getAcc()+ ","); // if write enabled, write to disk
printw.printf(tmp.getAcc()+ ",");
tmp.setAcc(0); // reset accumulator
}
}
我想要的是这样的:
相反,我得到:
这个想法是我想要每行数据的日期和时间戳,这就是第二行的用途。
我应该进行什么编辑?
编辑:我删除了屏幕截图并发布了代码片段。
解决方案
您应该使用 StringBuffer 来构建您的字符串,然后对最终结果执行 printf
StringBuffer sb = new StringBuffer();
sb.append(dtf.format(LocalDateTime.now()) + ",");
// Add your other items with sb.append()
// When done write out the end result
printw.printf(sb.toString());
同样正如 Andreas 在评论中提到的那样,您应该从中间追加中删除 \r\n ,并且只在最后添加它们。
推荐阅读
- php - localstack s3 无法从 php 访问
- python-3.x - 将一个函数的多个返回值作为参数传递给另一个函数
- bash - 通过 Bitrise 运行时,Bash shell 脚本命令乱序执行
- java - 虽然使用相同的数据提供者在 2 个不同的类中有 2 个测试用例,但在执行第 2 个用例时,超类中的所有值都会重置为 null
- java - T4CConnection.doDescribeTable() 调用每个插入语句?
- c++ - 为什么这个 C++ LinkedList 语句无效?
- c# - 实体框架添加记录而不检查它是否存在或已添加
- python-import - 如何让包/模块在 Python 3.9.1 中工作(包间和包外)
- python - 由于特殊符号无法合并数据帧
- java - 编译致命错误:无效的目标版本:12 -> [帮助 1]