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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T09:05:43+00:00 2026-06-04T09:05:43+00:00

I have a strange problem where I have a log file called transactionHandler.log.It is

  • 0

I have a strange problem where I have a log file called transactionHandler.log.It is a very big file having 17102 lines.This i obtain when i do the following in the linux machine:

wc -l transactionHandler.log
17102 transactionHandler.log

But when i run the following java code and print the number of lines i get 2040 as the o/p.

import java.io.*;
import java.util.Scanner;
import java.util.Vector;

public class Reader {

    public static void main(String[] args) throws IOException {     
        int counter = 0; 
        String line = null;

         // Location of file to read
        File file = new File("transactionHandler.log");

        try {

            Scanner scanner = new Scanner(file);

            while (scanner.hasNextLine()) {
                line = scanner.nextLine();
                System.out.println(line);
                counter++;                    
            }
            scanner.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }           
        System.out.println(counter);        
    }
}

Can you please tell me the reason.

  • 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-04T09:05:45+00:00Added an answer on June 4, 2026 at 9:05 am

    From what I know, Scanner uses \n as delimiter by default. Maybe your file has \r\n. You could modify this by calling scanner.useDelimiter or (and this is much better) try using this as an alternative:

    import java.io.*;
    
    public class IOUtilities
    {
        public static int getLineCount (String filename) throws FileNotFoundException, IOException
        {
            LineNumberReader lnr = new LineNumberReader (new FileReader (filename));
            while ((lnr.readLine ()) != null) {}
    
            return lnr.getLineNumber ();
        }
    }
    

    According to the documentation of LineNumberReader:

    A line is considered to be terminated by any one of a line feed
    (‘\n’), a carriage return (‘\r’), or a carriage return followed
    immediately by a linefeed.

    so it’s very adaptable for files that have different line terminating characters.

    Give it a try, see what it does.

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

Sidebar

Related Questions

We've faced strange problem. We have log on service, that authenticates user, adds auth
I'm having a strange problem and not sure how to troubleshoot it. I have
I have a very strange problem with a UITableview within a navigation controller on
I have encountered a very strange problem. The jquery slideshow in the below webpage
I have a strange problem with my app, sometimes, very rare in fact, when
I am having a strange problem when using this XML-to-NSDictionary class.really it was working
I have a very strange problem. (fyi - I'm using forms authentication and manually
I am having a strange problem with MySQL Stored Procedure. I have written a
I have a very very strange problem here. I have a desktop application written
I have a very strange problem in my code. I'm using Ogre and I'm

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.