Java - 将数组中的字符串转换为双精度
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2454137/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java - converting String in array to double
提问by cc0
I'm stuck with this pretty silly thing;
我被这个非常愚蠢的事情困住了;
I got a textfile like this;
我有一个这样的文本文件;
Hello::140.0::Bye
你好::140.0::再见
I split it into a string array using;
我将其拆分为一个字符串数组;
LS = line.split("::");
Then I try to convert the array values containing the number to a double, like this;
然后我尝试将包含数字的数组值转换为双精度值,如下所示;
Double number = Double.parseDouble(LS[1]);
But I get the following error message;
但我收到以下错误消息;
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
线程“main”中的异常 java.lang.ArrayIndexOutOfBoundsException: 1
Does anyone have any idea why this doesn't work?
有谁知道为什么这不起作用?
回答by Halo
I think it's a problem with reading the file. Did you try printing line and also the contents of LS after splitting?
我认为这是读取文件的问题。您是否尝试在拆分后打印行和 LS 的内容?
回答by anamitra deb
public class Test
{
public static void main(String[] args)
{
String s = "140.0::156.33::155.89";
String[] ls;
ls = s.split("::");
for(int i=0;i<=2;i++)
{
//System.out.println(ls[i]);
double d = Double.parseDouble(ls[i]);
System.out.println(d);
}
}
}
回答by Paul Tomblin
That error has nothing to do with that line of code. Look for somewhere where you're trying to access index 3 of an array. LS in this example should only have indexes 0-2.
该错误与那行代码无关。寻找您尝试访问数组索引 3 的地方。本例中的 LS 应该只有索引 0-2。
回答by Mnementh
I think some lines don't contain data in the specified format. Does the file contains empty lines? In this case the split returns only a single-element array. To find the problem, you could print the line (of the data), on which the error occurs.
我认为有些行不包含指定格式的数据。文件是否包含空行?在这种情况下,拆分只返回一个单元素数组。要找到问题,您可以打印发生错误的(数据的)行。
回答by Alphie
You should a compiler, such as Eclipse, which allows you to debug the code dynamically. The issue here is that the Array LS has a size <=1. This could be caused by incorrectly reading the text file as Halo mentioned or as Mnementh mentioned by having a blank line. This will infact occur for any line that does not contain the :: delimiter.
您应该使用编译器,例如Eclipse,它允许您动态调试代码。这里的问题是 Array LS 的大小 <=1。这可能是由于 Halo 提到的文本文件或 Mnementh 提到的文本文件错误地读取了空行造成的。这实际上会发生在任何不包含 :: 分隔符的行中。
You should perform a check to ensure that the Array LS contains >X entries, especially if you are reading in from a file where entries could vary.
您应该执行检查以确保 Array LS 包含 >X 个条目,尤其是当您从条目可能不同的文件中读取时。
double number = 0.0
if( LS.size() > 3 ){
number = Double.parseDouble(LS[1]);
}
回答by JAN
import java.util.ArrayList;
import java.util.Arrays;
public class StringToDoubles {
/**
* @param args
*/
public static void main(String[] args)
{
String CommaSeparated = "22.3 , 24.5, 44.3";
java.util.List<String> items = Arrays.asList(CommaSeparated.split("\s*,\s*"));
java.util.List<Double> out = new ArrayList<Double>();
for(int i=0; i < items.size();i++)
{
double d = Double.parseDouble(items.get(i));
System.out.println(d);
out.add(new Double(d));
}
}
}
回答by Yeabkal Wubshit
//Using streams provides a neat solution.
// this will give you {1.3, 3.5, 98.342}
String input = "1.3::3.5::98.342";
double[] arr = Arrays.stream(input.split("::")).mapToDouble(Double::parseDouble).toArray();
回答by Daniel Wedlund
what type is LS?? This code works for me:
LS是什么类型??这段代码对我有用:
public class Test
{
public static void main(String[] args)
{
String s = "Hello::140.0::Bye";
String[] ls;
ls = s.split("::");
System.out.println(ls[1]);
double d = Double.parseDouble(ls[1]);
System.out.println(d);
}
}

