Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8257245
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T02:08:00+00:00 2026-06-08T02:08:00+00:00

i want to parse a text file that represents a log. i want it

  • 0

i want to parse a text file that represents a log. i want it to be powerful enough to handle all erros that might occur.. although i am clueless about the best practices and the errors i should account for . i will be using JAVA to implement this.

Sample log :
2012-07-16 10:23:40,558 – 127.0.0.1 – Paremter array[param1=1,param2=1,param3=0,] – 383

I already wrote a prasing code that works as follows :

public Parser(String log) {
    this.log = log;
    this.parse();

}

public void parse() {

    String[] temp = new String[10];
    String[] temp2 = new String[10];

    temp = log.split(" - ");
    key = temp[3];
    id = Integer.parseInt(key);
    String IP = temp[1];
    String str;

    String temp3 = temp[2].substring(temp[2].indexOf("g"), temp[2].indexOf("]"));
    temp = temp3.split(",");
    str = "param1";

    boolean ordered = CheckOrder(temp);


    if (ordered) {
        for (int q = 0; q < temp.length; q++) {

            temp[q] = temp[q].substring(temp[q].indexOf("=") + 1);

        }



        if (temp[0].equals("q")) {
            param= 0;
        } else if (temp[0].equals("k")) {
            param= 1;
        } else {
            param= 2;
        }

         // Same way for all parameters

    }


}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T02:08:02+00:00Added an answer on June 8, 2026 at 2:08 am

    Check the javadoc of all the methods you use, and make sure to handle all the nominal and exceptional cases:

    • the file doesn’t exist: an exception is being thrown. Handle this exception correctly
    • String.indexOf() doesn’t find what it looks for. It returns -1. Handle this case correctly
    • String.split() doesn’t return an array of the length I expect. Handle this case correctly
    • …

    Split your big method into several sub-methods, each doing only one thing.

    Write unit tests to check that your methods do what they’re supposed to do, with all the possible inputs.

    Note that “handling things correctly” might very well mean: throw an exception because the input is incorrect, if the contract is that the logs follow a well-defined format. In this case, it’s the code generating the logs that is incorrect. But it’s better to have an exception telling which format you expected and which format you got instead, rather than an obscure NullPointerException or ArrayIndexOutOfBoundsException.

    The above applies to any kind of code you write, and not just to file parsing.

    Side note:

    String[] temp = new String[10];
    temp = log.split(" - ");
    

    What’s the point in creating an array of 10 elements to discard it right after and replace it by another array (the one returned by log.split(" - ")).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to parse a large text file so that it breaks into a
I want to parse text from a xml file.Consider that I have a some
I am using opencsv to parse text file and generate List<String[]> now I want
I'm trying to parse a text file using parser combinators. I want to capture
I want to parse the html file, pdf file, csv file and text file.Now
So I'm trying to a parse a file that has text in this format:
Perl newbie here. I have a log file that I need to parse out
I am trying to parse out a text file that looks like the following:
I want to parse a text file in a C program. The file contains
I'm trying to parse a text file that has a heading and the body.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.