簡體   English   中英

JFrame打印輸出不符合預期。 如果循環麻煩

[英]JFrame Printing out not as expected. if loop trouble

抱歉,代碼太長,不想遺漏任何內容。
小小的解釋,我只是想嘗試一種方法,讓用戶輸入日期以檢查兩個日期之間的歷史記錄
現在,我在一定程度上可以正常工作,它讀取一個文本文檔,如果日期在日期之間,則每個事務有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 ########

如果我輸入日期2015年5月15日至2015年5月16日
我得到:

######## 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);
        }
    });
}

}

抱歉,我這次知道了,以防萬一有人稍后再看,是因為我的for循環結束於7而不是8,並且沒有將值保存到數組[7]中,還因為日期檢查正在執行| | 下面是更新的類,而不是&&

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;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM