0

我正在使用 split 来划分字符串输入。前 3 个很好用,因为它们是字符串,但最后一个输入是双精度。知道我应该如何进行这项工作,以便字符串拆分可以将结果 [4] 传递给 ArrayList 中的双精度数吗?

Scanner catin = new Scanner(System.in);
System.out.print("Enter the Name, Breed, Color, and Weight of the Cat: ");
String test = catin.nextLine();
String[] result = test.split(","); 
Cat c4 = new Cat(result[1], result[2], result[3], result[4]);
4

2 回答 2

2

不太确定你想要什么,但我认为你在追求 Double.parseDouble(result[4])

Cat cat = new Cat(result[1], result[2], result[3], Double.parseDouble(result[4]));

再次注意索引。索引应该是0 --> 3数组是从零开始索引的。

于 2013-10-19T22:10:04.193 回答
1

你的索引是错误的。应该:

Cat c4 = new Cat(result[1], result[2], result[3], Double.parseDouble(result[4]));

数组是零索引的,并解析您的weight String.

于 2013-10-19T22:10:20.180 回答