Scanner input = new Scanner(new FileReader(selectFile.getSelectedFile()));
int count = 0;
System.out.println("SampleName,RecordingTime,PeakName,Amount");
while(input.hasNext() && count <= 1000){
count++;
String word = input.next();
if (count == 2){
System.out.print(word + ",");
}
if (count == 36){
System.out.print(word + ",");
}
if (count == 64){
System.out.print(word + ",");
}
if (count == 68){
System.out.print(word + ",");
}
}
如何打印序列中的下一个单词? 例如,程序读取如下文本文件。
你好
姓名:约翰 金额:12.0
如何在姓名后打印单词:在这种情况下约翰或金额:12.0 提前致谢。
答案 0 :(得分:0)
这是一种可以做到这一点的方法。
while (input.hasNext()) {
String word = input.next();
Pattern pattern1 = Pattern.compile("Name:");
Pattern pattern2 = Pattern.compile("Amount:");
Matcher matcher1 = pattern1.matcher(word);
Matcher matcher2 = pattern2.matcher(word);
if(matcher1.matches()){
System.out.println(input.next());
}
if(matcher2.matches()){
System.out.println(input.next());
}
}