2015-05-20 119 views
0

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

值:

左腿 时间(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 ....................... .......................

+1

Habe你试过什么吗? –

+1

什么是“缓冲区?”这个缓冲区? http://docs.oracle.com/javase/7/docs/api/java/nio/Buffer.html – grill

+0

是的,我试图使用“索引”功能和子字符串函数。但是每次运行代码时索引号都会改变。因此,我需要一些其他函数来获取“时间”和“陀螺仪”值,其中(inputStream.available()> 0)尝试输入数据的时间和“陀螺仪”值为 – Aleesha

回答

0

您可以分割每个String和提取值,你需要:

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

String firstRow =“Left Leg Time(ms):676589”; String secondRow =“陀螺仪:-1.20,-1.38,-3。05“;问题是我有一组值每毫秒进来,我的最终目标是计算步数,这意味着每毫秒我的时间和x,y,z值是不同的。从缓冲区而不是声明为上述 – Aleesha

+0

谢谢...它帮助我了 – Aleesha

0

作出上述解决方案更一般, 我会做以下几点:

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

谢谢,我会尝试 – Aleesha