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 8505847
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T02:26:04+00:00 2026-06-11T02:26:04+00:00

My program reads lines from a plain text file w/ lines formatted: <integer>;<integer>%n ,

  • 0

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])) {
           tallyArray[i]++;
        }
    }
} 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.

  • 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-11T02:26:06+00:00Added an answer on June 11, 2026 at 2:26 am

    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")));
    scan.useDelimiter(Pattern.compile("(;|\\s)"));
    
    for (int i = 0; i < tallyArraySize; i++) {
        int ref1 = scan.nextInt();
        int ref2 = scan.nextInt();
    
        if (IntReferenceVal1 == ref1 && 
            IntReferenceVal2 == ref2) {
           tallyArray[i]++;
        }
    }
    

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

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

Sidebar

Related Questions

I'm trying to write a little C# program that reads from a text file
I am writing a FORTRAN program that reads data from a text file and
I have a program that reads data from a file line-by-line. I would like
My SSIS program reads as input from a .csv file. The file has about
I have a program that reads from a file that grabs 4 bytes from
I have a program that reads input from a file. I am trying to
I'm working on a program for class that reads data from a file, the
I have a binary program developed for Linux which reads lines from a server's
I'm looking at this program that reads input lines and then sorts them, from
My Ruby program reads lines from stdin and uses puts to print to stdout

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.