我正在尝试按如下方式创建一个程序:
给出来自csv / text文件的数据,例如:
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
任何帮助将不胜感激
答案 0 :(得分:1)
您可以使用http://csvjdbc.sourceforge.net/并使用选择查询。或直接将行导入某个数据库(在内存或真正的大数据库中) - 对于cvs处理,有许多库,如opencsv和通过sql查询选择值。
答案 1 :(得分:1)
以下是如何实现它的示例。 Java实现尝试从给定的字符串创建Date对象并进行比较。
输入(Data.csv):
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
输出:
Entry [colour=red, date=03/11/2014, time=13:00, noise=10]
Entry [colour=blue, date=04/11/2014, time=14:00, noise=15]
Entry [colour=pink, date=03/11/2014, time=15:00, noise=50]
实现:
import java.io.File;
import java.io.FileNotFoundException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;
public class RangeCalculator {
private File file;
public static final String DATE_FORMAT = "dd/MM/yyyy";
public class Entry {
String colour;
String date;
String time;
String noise;
public Entry(String colour, String date, String time, String noise) {
super();
this.colour = colour;
this.date = date;
this.time = time;
this.noise = noise;
}
// implement getters and setters here if necessary
@Override
public String toString() {
return "Entry [colour=" + colour + ", date=" + date + ", time="
+ time + ", noise=" + noise + "]";
}
}
public RangeCalculator(String fileName) {
file = new File(fileName);
}
private List<Entry> computeRange(String from, String to)
throws FileNotFoundException, ParseException {
List<Entry> result = new LinkedList<>();
SimpleDateFormat formatter = new SimpleDateFormat(DATE_FORMAT);
Date fromDate = formatter.parse(from);
Date toDate = formatter.parse(to);
Scanner scanner = new Scanner(file);
scanner.useDelimiter("[,\n]");
// maybe you want/need to skip the first line of the file
// if (scanner.hasNextLine()) {
// scanner.nextLine();
// }
while (scanner.hasNextLine()) {
String color = scanner.next();
String date = scanner.next().substring(1);
String time = scanner.next().substring(1);
String noise = scanner.next().substring(1);
Date currDate = formatter.parse(date);
if (!currDate.before(fromDate) && !currDate.after(toDate)) {
result.add(new Entry(color, date, time, noise));
}
}
scanner.close();
return result;
}
private void printEntries(List<Entry> entries) {
for (Entry entry : entries) {
System.out.println(entry.toString());
}
}
public static void main(String[] args) {
RangeCalculator app = new RangeCalculator("Data.csv");
List<Entry> calculatedEntries = null;
try {
calculatedEntries = app.computeRange("03/11/2014", "04/11/2014");
} catch (FileNotFoundException e) {
System.err.println("ERROR: File not found!");
System.exit(1);
} catch (ParseException e) {
System.err.println("ERROR: Wrong date format!");
System.exit(1);
}
app.printEntries(calculatedEntries);
System.exit(0);
}
}