Click here to Skip to main content
15,867,834 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
I have a text file with this content
08:00 - 09:30
09:45 - 11:15
11:30 - 13:00
08/04/13
14:00 - 15:30
15:45 - 17:15
17:30 - 19:00








MIS (CH27) UM
MIS (CH27) UM


With each empty line corresponds to one day in week, 08/04/14 is the first day of the week. I want to read text file, calculate the day when MIS happen and print it out. Here is my program, but it just shows nothing in the result.

Java
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

public class UsingPattern {

public static final String SOURCE = "results/timetable1.txt";

/**
 * @param args
 * @throws ParseException 
 */
public static void main(String[] args) throws ParseException {      
    new UsingPattern().readFile(SOURCE);        
}
public void readFile(String filename) throws ParseException {       
    try {
        File file = new File(filename);
        FileReader reader = new FileReader(file);
        BufferedReader in = new BufferedReader(reader);
        String line;
        Calendar cal = new GregorianCalendar();                                 // Create a Calendar object
        while ((line = in.readLine()) != null) {                                // Read lines, check for end-of-file
            SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yy");     // Create a format for date input from text file                    
            Date date = null;
            if (line.matches("\\d{2}/\\d{2}/\\d{2}")) {                         // If the line has this format, put it as date
                date = dateFormat.parse(line);                                  
                cal.setTime(date);                                              // Sets this Calendar's time with the given Date.               
            }
            else {                                                              // Each empty line correspond to 1 day
                if ((in.readLine()) == " ") {                               
                    cal.add(Calendar.DAY_OF_MONTH, 1);                      
                }
                else {
                    if (line.matches("MIS")) {
                        String fday = dateFormat.format(cal.getTime()); 
                        System.out.println((fday)); 
                        System.out.println(line);
                    }   
                }                   
            }               
        }
        in.close();
    }       
    catch (IOException e) {
        e.printStackTrace();
    }

}



Could someone point out where did I do wrong! Thanh you!
Posted
Updated 23-Oct-13 14:13pm
v3
Comments
Sergey Alexandrovich Kryukov 23-Oct-13 19:17pm    
Run it under the debugger...
—SA
thongla 23-Oct-13 19:21pm    
It just terminated, nothing happend.
Kenneth Haugland 23-Oct-13 19:24pm    
Breakpoints?
Sergey Alexandrovich Kryukov 23-Oct-13 19:25pm    
Run it under the debugger... Then the debugging session will happen.
—SA
thongla 23-Oct-13 19:28pm    
Could you show me how to do that ?
I just press F11 in Eclipse.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900