Is this a good way of parsing a string?

My program reads lines from a plain text file w/ lines formatted: <integer>;<integer>%n, where ; is the delimiter. It compares the two parsed integers against 2 other known values and increments tallyArray[i] if they match.

I currently use:

try {
    scan = new Scanner(new BufferedReader(new FileReader("LogFileToBeRead.txt")));

    for (int i = 0; i < tallyArraySize; i++) {
        explodedLogLine = scan.nextLine().split(";");

        if (IntReferenceVal1 == Integer.parseInt(explodedLogLine[0]) && IntReferenceVal2 == Integer.parseInt(explodedLogLine[1])) {
} finally {
    if (scan != null) { scan.close(); }

I was wondering if there were any serious faults with this method. It does not need to be production-quality.

Also, is there a standard way of parsing a string like this?

EDIT: We can assume the text file is perfectly formatted. But I see the importance for accounting for possible exceptions.


You are not handling NumberFormatExceptions thrown by the Integer.parseInt() method calls. If there's one bad line, execution exits your for loop.

You aren't vetting the integrity of the file you are reading from. If there isn't a ; character or if the Strings aren't actually numbers, execution simply exits the code block you posted.

If you can assume the file is perfectly formatted, and you're set on using a Scanner, you can add ; as a delimiter to the Scanner:

scan = new Scanner(new BufferedReader(new FileReader("LogFileToBeRead.txt")));

for (int i = 0; i < tallyArraySize; i++) {
    int ref1 = scan.nextInt();
    int ref2 = scan.nextInt();

    if (IntReferenceVal1 == ref1 && 
        IntReferenceVal2 == ref2) {

And simply call Scanner.nextInt() twice for each line.

According to me There are three flaws in the program.

  1. Delimiter ; what if there is delimiter is removed by accident or added by accident
  2. There should be check on explodedLogLine that it is of length 2 and it is not null otherwise it will result in unexpected runtime error
  3. You should catch NumberFormatException format exception since you can never be sure that Input is always a number

A simple illustration below gives you idea how things will go wrong.

    String str = "3;;3";

This code will print [3, , 3] in such case your program will produce NumberFormatException as "" string can not be parsed to Integer.

Need Your Help

Get all assemblies from a solution when using t4 templates, not the current executing assembly

c# visual-studio reflection t4

I'm using T4 templates to try and output all the assemblies used in my solution. However, in the .tt file, all the loaded assemblies have nothing to do with the parent project or solution as I pres...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.