1

I am trying to read a log file and trying to print all the logs between a certain dates but i end up getting this exception when am trying to retrieve date from the log. this is my code and it is actually printing some log messages

public class Teat {


public static void main(String[] args) throws Exception {
    // TODO Auto-generated method stub
    {
        // TODO Auto-generated method stub
        SimpleDateFormat formatter = new SimpleDateFormat("dd MMM yyyy");
        BufferedReader br = new BufferedReader(new FileReader(
                "C:\\log.log"));
        String sCurrentLine;
        try {
            String startDate = "12 Dec 2013";
            String endDate = "12 Dec 2013";

            Date dateStart = formatter.parse(startDate);
            Date dateEnd = formatter.parse(endDate);
            Date logDate = null;
            int sDay = dateStart.getDate();
            int sMonth = dateStart.getMonth();
            int sYear = dateStart.getYear();
            int eDay = dateEnd.getDate();
            int eMonth = dateEnd.getMonth();
            int eYear = dateEnd.getYear();
            String date;
            int i=0;
              ArrayList<String> Sub_string = new ArrayList<String>();
            do {
                sCurrentLine = br.readLine();
                Sub_string.add(sCurrentLine.substring(0, 11));

                logDate = formatter.parse(Sub_string.get(i));
                int lDay = logDate.getDate();
                int lMonth = logDate.getMonth();
                int lYear = logDate.getYear();
                if (lYear >= sYear && lYear <= eYear) 
                {
                    if (lMonth >= sMonth && lMonth <= eMonth)

                    {
                        if (lDay >= sDay && lDay <= eDay) {
                            System.out.println(sCurrentLine);
                        }
                    }
                }
                else{System.out.println("pls ented valid dates");}
                i++;
            }while(sCurrentLine!=null);
        }

        catch (ParseException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }

    }
}

}

error is

 Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index 
out of range: 11
    at java.lang.String.substring(Unknown Source)
    at Teat.main(Teat.java:41)
6
  • Sub_string.add(sCurrentLine.substring(0, 11)); what is the value of sCurrentLine here ? Commented Dec 19, 2013 at 6:11
  • the line that i read from the log file, Commented Dec 19, 2013 at 6:12
  • yes what is the value of that line ? Commented Dec 19, 2013 at 6:12
  • the value is 12 Dec 2013 Commented Dec 19, 2013 at 6:15
  • why don't you use regex for that? Commented Dec 19, 2013 at 6:20

4 Answers 4

2

Change whlie((sCurrentLine = br.readLine())!=null) instead of do while otherwise you will end with NullPointerException.

and check if(sCurrentLinet.isEmpty()&&sCurrentLinet.length()>=11) before processing.

Sign up to request clarification or add additional context in comments.

Comments

2

yes what is the value of that line ? – Jigar Joshi 3 mins ago edit
the value is 12 Dec 2013 – user3117965 19 secs ago

12 Dec 2013 is 11 character long String so the last character is at the position 10 (first character is at 0 index), accessing 11th element will give put you out of bound and so the Exception

2 Comments

the size of the string is some 100+ but still i am getting the same problem
it could be good for some cases, but when it is the value you mentioned, it will fail with exception stopping the program to progress in this unhandled case
1
Sub_string.add(sCurrentLine.substring(0, 11));

is giving problem. Add a check before calling the substring(start, end) function

if (sCurrentLine != null && end>= start && end <= sCurrentLine.length())

Comments

1

Did you check whether all the lines in the log are atleast 11 characters long? You will get StringOutOfBoundsException if any of the lines are less than 11 chars in your case. Also please change your code according to the suggestion from @Prabhakaran.

1 Comment

It s actually a problem with the log file format thanks guys anyway

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.