0

I am attempting to access two string values within a simulation.log file and convert the two values to a type long. This is an example of the log file:

Log Fille

When I attempt to access the strings it is telling myself the are empty values. The error I am getting is:

Error

My code is as follows (I understand the statement is not fully closed):

File file = new File(simulationLogDir + "/simulation.log");
        FileReader simulationLogReader = new FileReader(file);
        BufferedReader bufferedReader = new BufferedReader(simulationLogReader);
        StringBuffer stringBuffer = new StringBuffer();
        String line;
        while ((line = bufferedReader.readLine()) != null) {
            String[] fields = line.split("\t");
            long responseTime = Long.parseLong(fields[5]) - Long.parseLong(fields[4]);
            if (line.startsWith("REQUEST")) {
                if (fields[7].equals("OK")) {
                    addPassedTest(fields[1], new Request(fields[4],responseTime, fields[7]));
                }

Each line in the simulation log is separated by tabbed spaces so the fields array separates each string.

1 Answers1

0

TO SUMMARIZE:

Double check your file for mistyped spaces.

Based on what I have noticed. Line 8 (Two parseLongs) is really vulnerable to exception, or we can say can throw NumberFormatException especially when it parses non-numerical string.

When does it happen? Based on the title of the question Exception in thread “main” java.lang.NumberFormatException: For input string: “ ”, I'm pretty sure you an instance of a line in your file that has a space right after a tab and coincidentally parsed by a Long.parseInt(string num);

To explain it even further, I'll give you 2 Sample Situations.

SITUATION 1: There is a line in your file that look like this

REQUEST (tab) test_12345_54321 (tab) 1 (tab) Macbook Request (tab) (space) 1490183061440 (tab) 1490183061321 (tab) OK

This line will then be splitted in such manner as follows:

fields[0] = REQUEST
fields[1] = test_12345_54321
fields[2] = 1
fields[3] = Macbook Request
fields[4] = (space) 1490183061440
fields[5] = 1490183061321
fields[6] = OK

Parsing 4th index and fifth index will be:

Long.parseLong(fields[4]) -> space can't be parsed as results to NumberFormatException
Long.parseLong(fields[5]) = 1490183061321 -> works fine


SITUATION 2: There is a line in your file that look like this

REQUEST (tab) test_12345_54321 (tab) 1 (tab) Macbook Request (tab) 1490183061440 (tab) (space) (tab) 1490183061321 (tab) OK

This line will then be splitted in such manner as follows:

fields[0] = REQUEST
fields[1] = test_12345_54321
fields[2] = 1
fields[3] = Macbook Request
fields[4] = 1490183061440
fields[5] = (space)
fields[5] = 1490183061321
fields[6] = OK

Parsing 4th index and fifth index will be:

Long.parseLong(fields[4]) = 1490183061440 -> works fine
Long.parseLong(fields[5]) -> space can't be parsed as results to NumberFormatException

Community
  • 1
  • 1
Jimwel Anobong
  • 468
  • 5
  • 18