很抱歉长代码,不想留下任何东西。
很少解释,我只是试图做一个用户输入日期来检查两个日期之间的历史的方式。现在我让它工作到一定程度,它读取文本文档,每个交易如果日期在日期之间,则需要打印8行
示例文档的结构如下:
######## START OF TRANSACTION ########
Name: name
DATE: 14/05/2015
Ammount: 100
Address: address
Card Number: 123312
ExpiryDate: 123312
######## END OF TRANSACTION ########
######## START OF TRANSACTION ########
Name: name
DATE: 19/05/2015
Ammount: 100
Address: address
Card Number: 123312
ExpiryDate: 123312
######## END OF TRANSACTION ########
如果我输入日期15-05-2015到16-05-2015
我明白了:
######## START OF TRANSACTION ########
Name: name
DATE: 14/05/2015
Ammount: 100
Address: address
Card Number: 123312
ExpiryDate: 123312
null######## END OF TRANSACTION ########
######## START OF TRANSACTION ########
Name: name
DATE: 19/05/2015
Ammount: 105
Address: address
Card Number: 123312
null
1:猜测我的循环的NULL值是什么 2:为什么它打印19.即使我有它的第16个仍然打印19 ....请帮助,我已经在这一段时间
import javax.swing.*;
import java.awt.Dialog.ModalityType;
import java.awt.event.*;
import java.awt.*;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
public class TopUpHistoryScreen extends JDialog {
private JPanel mainPanel;
private JTextArea historyScreen;
public TopUpHistoryScreen()
{
setPanels();
setModalityType(ModalityType.APPLICATION_MODAL);
setSize(400, 450);
setVisible(true);
}
public String Reader() {
try {
ArrayList<String> Trains = new ArrayList<String>();
int count = 0;
String testing = "";
File file = new File("TopUp.txt");
FileReader fileReader = new FileReader(file);
BufferedReader bufferedReader = new BufferedReader(fileReader);
StringBuffer stringBuffer = new StringBuffer();
String line;
while ((line = bufferedReader.readLine()) != null)
{
stringBuffer.append(line);
count += count;
Trains.add(line + "\n");
stringBuffer.append("\n");
testing += line + "\n";
//field.setText(line);
}
fileReader.close();
return testing;
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
public String checkDates(String startDate, String endDate) {
try {
ArrayList<String> Lines = new ArrayList<String>();
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
int count = 0;
int check = 0;
String[] lineArray = new String[8];
String DateSelected = "";
String testing="";
File file = new File("TopUp.txt");
FileReader fileReader = new FileReader(file);
BufferedReader bufferedReader = new BufferedReader(fileReader);
StringBuffer stringBuffer = new StringBuffer();
String line;
while ((line = bufferedReader.readLine()) != null)
{
lineArray[count] = line+"\n";
if (count < 8){
count ++;
}
if (count == 7){
if(check == 1){
testing += lineArray[0];
testing += lineArray[1];
testing += lineArray[2];
testing += lineArray[3];
testing += lineArray[4];
testing += lineArray[5];
testing += lineArray[6];
testing += lineArray[7];
check = 0;
}
}
if (count == 7){
count = 0;
check = 0;
lineArray = new String[8];
}
if (line.contains("DATE")){
try {
DateSelected = line.replace("DATE: ", "");
Date MainDate = sdf.parse(DateSelected);
Date SDate = sdf.parse(startDate);
Date EDate = sdf.parse(endDate);
if(MainDate.compareTo(SDate)>=0 || MainDate.compareTo(EDate)<=0 ){
//is after Sdate and before eDate
check = 1;
}
// if(MainDate.compareTo(SDate)<0){ //is Before SDate
//
// }
// if(MainDate.compareTo(SDate)==0){ //is equal to mainDate
//
// }
} catch (ParseException e) {
//e.printStackTrace();
}
}
stringBuffer.append(line);
Lines.add(line + "\n");
stringBuffer.append("\n");
}
fileReader.close();
return testing;
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
public void setPanels()
{
mainPanel = new JPanel(new GridLayout(0, 2));
JPanel containerPanel = new JPanel(new GridLayout(0, 1));
JPanel lowerPanel = new JPanel(new FlowLayout());
//JButton apply = new JButton("Select data area");
JButton exit = new JButton("Okay!");
exit.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
dispose();
}
});
JButton checkDate = new JButton("check dates");
JLabel START = new JLabel("START DATE!");
JLabel startDay = new JLabel("Day:");
JTextField sDay = new JTextField();
JLabel startMonth = new JLabel("Month:");
JTextField sMonth = new JTextField();
JLabel startYear = new JLabel("Year:");
JTextField sYear = new JTextField("2015");
JLabel END = new JLabel("END DATE!");
JLabel endDay = new JLabel("Day:");
JTextField eDay = new JTextField();
JLabel endMonth = new JLabel("Month:");
JTextField eMonth = new JTextField();
JLabel endYear = new JLabel("Year:");
JTextField eYear = new JTextField("2015");
//JTextField Data = new JTextField();
//JTextField touchOnTimeFieldminute = new JTextField();
historyScreen = new JTextArea(10,20);
JScrollPane scrolll = new JScrollPane(historyScreen);
//mainPanel.add(SelectData);
//mainPanel.add(SelectData);
// mainPanel.add(new JLabel());
mainPanel.add(new JLabel());
mainPanel.add(START);
mainPanel.add(startDay);
mainPanel.add(sDay);
mainPanel.add(startMonth);
mainPanel.add(sMonth);
mainPanel.add(startYear);
mainPanel.add(sYear);
mainPanel.add(new JLabel());
mainPanel.add(END);
mainPanel.add(endDay);
mainPanel.add(eDay);
mainPanel.add(endMonth);
mainPanel.add(eMonth);
mainPanel.add(endYear);
mainPanel.add(eYear);
mainPanel.add(new JLabel());
mainPanel.add(checkDate);
lowerPanel.add(scrolll);
lowerPanel.add(exit);
containerPanel.add(mainPanel);
containerPanel.add(lowerPanel);
add(containerPanel);
checkDate.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
String startday = sDay.getText();
String startmonth = sMonth.getText();
String startyear = sYear.getText();
String endday = eDay.getText();
String endmonth = eMonth.getText();
String endyear = eYear.getText();
String startDate = (startday+"/"+startmonth+"/"+startyear);
String endDate = (endday+"/"+endmonth+"/"+endyear);
String AnswerDates = checkDates(startDate,endDate);
historyScreen.setText(AnswerDates);
}
});
}
}
答案 0 :(得分:0)
对不起伙计这次我得到了它,只是因为有人看到这个后来哈哈哈,这是因为我的for循环结束于7而不是8并且没有保存值到数组[7],还因为日期检查正在做||而不是&amp;&amp;下面是更新的课程
public String checkDates(String startDate, String endDate) {
try {
ArrayList<String> Lines = new ArrayList<String>();
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
int count = 0;
int check = 0;
String[] lineArray = new String[8];
String DateSelected = "";
String testing="";
File file = new File("TopUp.txt");
FileReader fileReader = new FileReader(file);
BufferedReader bufferedReader = new BufferedReader(fileReader);
StringBuffer stringBuffer = new StringBuffer();
String line;
while ((line = bufferedReader.readLine()) != null)
{
lineArray[count] = line+"\n";
if (count < 8){
count ++;
}
if (count == 8){
if(check == 1){
testing += lineArray[0];
testing += lineArray[1];
testing += lineArray[2];
testing += lineArray[3];
testing += lineArray[4];
testing += lineArray[5];
testing += lineArray[6];
testing += lineArray[7];
check = 0;
}
}
if (count == 8){
count = 0;
check = 0;
lineArray = new String[8];
}
if (line.contains("DATE")){
try {
DateSelected = line.replace("DATE: ", "");
Date MainDate = sdf.parse(DateSelected);
Date SDate = sdf.parse(startDate);
Date EDate = sdf.parse(endDate);
if(MainDate.compareTo(SDate)>=0 && MainDate.compareTo(EDate)<=0 ){
//is after Sdate and before eDate
check = 1;
}
// if(MainDate.compareTo(SDate)<0){ //is Before SDate
//
// }
// if(MainDate.compareTo(SDate)==0){ //is equal to mainDate
//
//
}
} catch (ParseException e) {
//e.printStackTrace();
}
}
stringBuffer.append(line);
Lines.add(line + "\n");
stringBuffer.append("\n");
}
fileReader.close();
return testing;
} catch (IOException e) {
e.printStackTrace();
}
return null;
}