[英]Split comma and one by one value show on android
String ecg1 = "10,20,100,-100,20,10,110,-105,15,25";
Double.parseDouble(model.getEcg1().split(",") + "\n")
This is not working.这是行不通的。 I want the result like below ie value one by one column wise:
我想要如下结果,即逐列计算:
10
20
100
-100
20
10
110
-105
15
25
split
will create a array of String
, and not a single String
, so you can't parse it to Double
. split
将创建一个String
数组,而不是单个String
,因此您无法将其解析为Double
。 After splitting the ecg1
, you can iterate through the list and print it.拆分
ecg1
后,您可以遍历列表并打印它。
try {
String ecg1 = "10,20,100,-100,20,10,110,-105,15,25";
String[] items = ecg1.split(",");
ArrayList<Double> numbers = new ArrayList();
for (int i=0; i<items.length; i++) {
numbers.add(Double.parseDouble(items[i]));
}
} catch (NumberFormatException e) {
e.printStackTrace();
}
You can do anything you want with the list of double numbers.你可以用双数列表做任何你想做的事情。
Double.parseDouble
can throw exception so that you should catch it. Double.parseDouble
可以抛出异常,以便您捕获它。
If you just want to generate a string which values are displayed as a column, there will be a simpler solution如果您只想生成一个将值显示为列的字符串,那么会有一个更简单的解决方案
String ecg1 = "10,20,100,-100,20,10,110,-105,15,25";
String result = ecg1.replace(",", "\n");
String ecg1 = "10,20,100,-100,20,10,110,-105,15,25";
String[] ecg2=ecg1.split(",");
then you can iterate through ecg2 with a normal loop然后您可以使用正常循环遍历 ecg2
for(int i=0;i<ecg2.length;i++)
{
//ecg2[i] is the element you want
}
If you use Java 8 you just can do如果你使用 Java 8 你就可以
String str = String.join("\n", ecg1.split(','));
System.out.println(str);
Try converting the given string into String Array and then insert the items by parsing into Double data type Like in the code given below:-尝试将给定的字符串转换为字符串数组,然后通过解析为 Double 数据类型来插入项目,如下面的代码所示:-
String[] items = ecg1.split(",");
ArrayList<Double> numbers = new ArrayList<Double>();
for (int i=0;i<items.length;i++) {
numbers.add(Double.parseDouble(items[i]));
}
This will not work as model.getEcg1().split(",")
will return a string array.这不起作用,因为
model.getEcg1().split(",")
将返回一个字符串数组。 You can not parse whole string array to double by this Double.parseDouble(model.getEcg1().split(","))
and adding \n
is again not going to work.您无法通过此
Double.parseDouble(model.getEcg1().split(","))
将整个字符串数组解析为加倍,并且添加\n
再次不起作用。
if you only want the desired output to print you can try this below code如果您只想打印所需的 output 可以尝试以下代码
String ecg1 = "10,20,100,-100,20,10,110,-105,15,25";
Stream<String> stream = Arrays.stream(ecg1.split(","));
stream.forEach(x -> System.out.println(x));
if you want to store in Collection do as below如果您想存储在 Collection 中,请执行以下操作
String ecg1 = "10,20,100,-100,20,10,110,-105,15,25";
List<Double> li=new ArrayList<>();
Stream<String> stream = Arrays.stream(ecg1.split(","));
stream.forEach(x -> li.add(Double.parseDouble(x)));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.