我正在编写一个与" Transactions.txt"交互的程序。文件的格式如下。
TransactionNumber,ItemName,Quantity,Purchase/HireDate,ReturnDate
其内容为:
T01,Jumper,5,10/03/2015,14/03/2015
T02,Tuxedo,1,21/04/2015,23/05/2015
T03,Jacket,2,02/07/2015,09/07/2015
T04,Hat,3,12/09/2015,19/09/2015
T05,Shoes,4,09/11/2015,20/11/2015
该计划的目标是获取开始日期并检查文件中的所有日期,以查看开始日期是否在任何日期之前,如果是,它将添加项目名称,它的数量和它的购买/雇用日期以及它返回要打印的字符串的日期。例如,如果我输入10/03/2015作为开始日期,则应打印所有内容,但只打印出第1,2和4行,我不知道问题出在哪里。
我还尝试添加一段将在结束日期到达的代码,并检查文件中的任何日期是否在结束日期之后,如果是,则输出将被打印然后它应该完成执行main(不是exit(0)因为我与其他类交互。所以如果我然后输入10/03/2015作为开始日期和15/03/2015作为结束日期,只有行将打印1的内容。
我已经尝试解决这个问题几个小时了,我无法找到问题所在,任何帮助都会非常感激。
import java.io.*;
import java.util.*;
import java.text.*;
public static File transactionsFile = new File("Transactions.txt");
public class Transaction
{
public static void main() throws IOException
{
try
{
String details = "";
Date tempDate;
SimpleDateFormat SDF = new SimpleDateFormat("dd/mm/yyyy");
System.out.print("Enter in a start date (dd/mm/yyyy): ");
String sDate = userInput.nextLine();
Date startDate = SDF.parse(sDate);
System.out.print("Enter in an end date (dd/mm/yyyy): ");
String eDate = userInput.nextLine();
Date endDate = SDF.parse(eDate);
List<String[]> contents = new ArrayList<>();
Scanner searchTransactionsFile = new Scanner(transactionsFile);
System.out.println("\n---------Summary of Transactions---------");
while(searchTransactionsFile.hasNextLine())
{
String[] current = searchTransactionsFile.nextLine().split(",");
contents.add(current);
}
for(String[] elements: contents)
{
tempDate = SDF.parse(elements[3]);
if(tempDate.after(startDate) || tempDate.equals(startDate))
details += "\nItem Name:\t\t" + elements[1] + "\nQuantity:\t\t" + elements[2] + "\nHire/Purchase Date:\t" + elements[3] + "\nReturn Date:\t\t" + elements[4] + "\n";
}
System.out.print(details);
searchTransactionsFile.close();
}
catch(Exception e)
{
System.out.println("Invalid date entered. Please enter a valid date in the format dd/mm/yyyy");
}
}
}
答案 0 :(得分:2)
把这一行
public static File transactionsFile = new File("Transactions.txt");
内部类定义。
您必须定义userInput
变量。像这样
Scanner userInput = new Scanner(System.in);
此外,您必须将日期格式修改为:
SimpleDateFormat SDF = new SimpleDateFormat("dd/M/yyyy");
因为mm
表示分钟而M
表示月份;
答案 1 :(得分:1)
你的条件错了。它没有考虑结束日期。我已经改变逻辑来考虑startDate和endDate。看看吧。
if(tempDate.after(startDate)&amp;&amp; tempDate.before(endDate))
import java.io.*;
import java.util.*;
import java.text.*;
public class Transaction {
public static File transactionsFile = new File("Transactions.txt");
public static void main(String args[]) throws IOException {
try {
String details = "";
Date tempDate;
SimpleDateFormat SDF = new SimpleDateFormat("dd/mm/yyyy");
System.out.print("Enter in a start date (dd/mm/yyyy): ");
Scanner userInput = new Scanner(System.in);
String sDate = userInput.nextLine();
Date startDate = SDF.parse(sDate);
System.out.print("Enter in an end date (dd/mm/yyyy): ");
String eDate = userInput.nextLine();
Date endDate = SDF.parse(eDate);
List<String[]> contents = new ArrayList<>();
Scanner searchTransactionsFile = new Scanner(transactionsFile);
System.out.println("\n---------Summary of Transactions---------");
while (searchTransactionsFile.hasNextLine()) {
String[] current = searchTransactionsFile.nextLine().split(",");
contents.add(current);
}
for (String[] elements : contents) {
tempDate = SDF.parse(elements[3]);
if (tempDate.after(startDate) && tempDate.before(endDate))
details += "\nItem Name:\t\t" + elements[1]
+ "\nQuantity:\t\t" + elements[2]
+ "\nHire/Purchase Date:\t" + elements[3]
+ "\nReturn Date:\t\t" + elements[4] + "\n";
}
System.out.print(details);
searchTransactionsFile.close();
} catch (Exception e) {
System.out
.println("Invalid date entered. Please enter a valid date in the format dd/mm/yyyy");
}
}
}
输出:
D:\Study\Examples\src>java Transaction
Enter in a start date (dd/mm/yyyy): 10/03/2015
Enter in an end date (dd/mm/yyyy): 15/03/2015
---------Summary of Transactions---------
Item Name: Hat
Quantity: 3
Hire/Purchase Date: 12/09/2015
Return Date: 19/09/2015