如何编辑字符串中的标记?

时间:2016-04-14 00:14:13

标签: java string token text-processing

我想在文本文件中编辑统一数据列表,这是一个数据样本:

Input File

我想格式化数据,使其如下所示:

Output File

到目前为止,我已经能够通过扫描仪读取每一行和令牌扫描程序,但我不知道如何编辑每个令牌以便

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class Data { 

public static void main(String[] args) throws FileNotFoundException {
    Scanner scanner = new Scanner(new File("PortlandWeather2014.txt"));

    int lineNumber = 1;
    while(scanner.hasNextLine()){
        String line = scanner.nextLine();

        Scanner token = new Scanner(line); 
        while(token.hasNext()){
            if (token.hasNext()) {
                String tokenS = token.next();
        }

        lineNumber++;

            }
        }
    }
}

我想将每个令牌存储在“tokenS”中,并使用if语句在第二个imgur链接中编辑相应的格式,以确定“tokenS”是字符串还是双精度,但我不确定该怎么做此

1 个答案:

答案 0 :(得分:0)

考虑

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

    String tokens [] = line.split (" ");  // space or tab?
    StringBuilder sb = new StringBuilder ();
    while (String tok : tokens) {
      sb.append (tok).append ("\t");  // maybe tok needs formatting
      // can test if tok is double maybe by using 
      // Double#parseDouble(java.lang.String)
    }

    // now print sb
    System.out.println (sb);

    }
}