如何从文本文件中提取数字 [英] How to extract numbers from a text file

查看:162
本文介绍了如何从文本文件中提取数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个看起来像这样的文本文件:

I have a text file that looks like:


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

我不明白的是如何只提取7个数字而不是日期。

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

到目前为止EDITcode:

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]);


推荐答案

字符串#split开头将值拆分为单独的元素...

Start with String#split to split the values into seperate elements...

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...
}

您还应该查看 JavaDocs 字符串追踪了解更多详情

You should also check out the JavaDocs and Strings trail for more details

这篇关于如何从文本文件中提取数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆