我已经在单元格++中创建了一个双打的ArrayList,它应该存储扫描程序检测到的所有双精度值。但是,当我运行代码时,程序会一直要求下一个输入,我不知道为什么。
例如,如果我的输入是2,3,9,我希望程序将值2存储到arraylist的第一个单元格,将值3存储到第二个单元格,将值9存储到第三个单元格。问题是,如果我在我的交互窗格中填写2,3,9,它会一直要求下一个输入。
ArrayList<Double> doubleList;
doubleList = new ArrayList<Double>();
while (scanner.hasNextDouble()) {
doubleList.add(scanner.nextDouble());
}
答案 0 :(得分:0)
Is this what you want to do? Hope it helps.
I have a test.txt file
and it has
2
3
4
9
running will give you output:
[2.0]
[2.0, 3.0]
[2.0, 3.0, 4.0]
[2.0, 3.0, 4.0, 9.0]
public class test2 {
public static void main(String[] args) throws FileNotFoundException
{
ArrayList<Double> doubleList;
doubleList = new ArrayList<Double>();
File myFile = new File("C:\\test.txt");
Scanner scanner = new Scanner(myFile);
while (scanner.hasNext() ) {
if (scanner.hasNextDouble())
doubleList.add(scanner.nextDouble());
System.out.println(doubleList.toString());
}
}
}