gpt4 book ai didi

java - 如何从文本文件中提取数字

转载 作者:行者123 更新时间:2023-12-01 13:51:10 25 4
gpt4 key购买 nike

我有一个文本文件,如下所示:

1/1/2009    76.0    81.1    68.1    86.7    99.2    97.5    92.9

What I don't understand is how to pull out just the 7 numbers and not the date.

EDITcode so far:

When i run it nothing is printed?

File inputFile = new File ("C:/Users/Phillip/Documents/Temp/temperatures.txt .txt");
Scanner scan = new Scanner(inputFile);


while (scan.hasNextLine())
{
String line = scan.nextLine();

String[] words = line.split(" ");

for (int index = 1; index < words.length; index++)
System.out.println(words[index]);

最佳答案

String#split 开始将值拆分为单独的元素...

String text = "1/1/2009 76.0 81.1 68.1 86.7 99.2 97.5 92.9";
String[] parts = text.split(" ");
// You can now skip over the first element and process the remaining elements...
for (int index = 1; index < parts.length; index++) {
// Convert the values as required...
}

您还应该查看 JavaDocsStrings trail了解更多详情

关于java - 如何从文本文件中提取数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19942076/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com