我正在尝试创建一个程序,如下所示:
给定来自 csv/文本文件的数据,例如:
Colour, date, time, noise
red, 03/11/2014, 13:00, 10
blue, 04/11/2014, 14:00, 15
pink, 03/11/2014, 15:00, 50
blue, 05/11/2014, 14:00, 15
如何返回 15:00 – 17:00 的范围,打印出该范围内的行并确保包含 17:00 的行也返回
因此,如果用户输入 03/11/2014 和 04/11/2014.. 其将输出:
red, 03/11/2014, 13:00, 10
blue, 04/11/2014, 14:00, 15
pink, 03/11/2014, 15:00, 50
这是我的尝试:
package readFile;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class read {
public static void main(String[] args) {
// TODO code application logic here
File fileName = new File("Data.csv");
try {
String startToken = "03/11/2014";
String endToken = "04/11/2014";
boolean output = false;
Scanner scan = new Scanner(fileName);
while (scan.hasNextLine()) {
String line = scan.nextLine();
if (!output && line.indexOf(startToken) >= -1) {
output = true;
line = line.substring(line.indexOf(startToken)+startToken.length());
} else if (output && line.indexOf(endToken) > -1) {
output = false;
System.out.println(line.substring(0, line.indexOf(endToken)));
}
if (output) {
System.out.println(line);
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
但是输出是:
ate, time, noise
red, 03/11/2014, 13:00, 10
blue,
, 15:00, 50
blue, 05/11/2014, 14:00, 15
任何帮助将不胜感激
最佳答案
您可以使用http://csvjdbc.sourceforge.net/并使用选择查询。或者直接将行导入到某个数据库中(在内存中或真正的大数据库中) - 对于 cvs 处理,有很多库(例如 opencsv)并通过 sql 查询选择值。
关于java - Java中查找范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27136059/