首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从缓冲区中提取字符串

从缓冲区中提取字符串
EN

Stack Overflow用户
提问于 2015-05-20 04:16:33
回答 2查看 287关注 0票数 0

下面是我的arduino脚本的输出。我使用Java (Netbeans )来计算下面一组值中的步骤数。我将这组值存储在缓冲区中。我只想使用java提取时间和陀螺仪x、y、z值。我记得有一种方法可以指向“时间”并添加索引号。但我对that.How不太确定,我能这么做吗?请帮帮忙

存储在缓冲区中的值:

左腿时间(Ms):676589

陀螺仪:-1.20,-1.38,-3.05

加速度计:-0.03,-0.12,-1.05

磁强计: 0.35,0.32,-0.26

右腿

时间(Ms):222875

陀螺仪: 1.53,-0.46,-2.21

加速度计: 0.29,-0.69,0.63

磁强计: 0.34,-0.31,-0.01

左腿

时间(Ms):676710

陀螺仪:-1.37,-1.22,-3.15

加速度计:-0.03,-0.12,-1.05

磁强计: 0.35,0.32,-0.26 ..............................................

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-05-20 04:21:28

可以拆分每个字符串并提取所需的值:

代码语言:javascript
复制
String firstRow = "Left Leg Time(ms): 676589";
String secondRow = "Gyroscope : -1.20 , -1.38 , -3.05";

String[] firstRowParts = firstRow.split(" ");
int time = Integer.parseInt(firstRowParts[3]);   // 676589
String[] secondRowParts = secondRow.split(" ");
int x = Integer.parseInt(secondRowParts[2]);     // -1.20
int y = Integer.parseInt(secondRowParts[4]);     // -1.38
int z = Integer.parseInt(secondRowParts[6]);     // -3.05
票数 0
EN

Stack Overflow用户

发布于 2015-05-20 04:32:42

为了使上述解决方案更一般化,我将做以下工作:

代码语言:javascript
复制
int time = 0;
int x = 0;
int y = 0;
int z = 0;
String[] lines = buffer.split("\n")
//you will have the lines here, assuming that you have the Buffer values  in a string called buffer
for(String string in lines){
   if (string.contains("Time")){
     String[] values = string.split(" ");
     time = Integer.parseInt(values[1]);  
   }
   if (string.contains("Gyroscope")){
     String[] values = string.split(" ");
     x = Integer.parseInt(values[1]);   
     y = Integer.parseInt(values[2]);   
     z = Integer.parseInt(values[3]);  
   }
 }

我没有测试它,所以我希望里面没有错误。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30340169

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档