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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T22:12:40+00:00 2026-05-11T22:12:40+00:00

In java i would like to read a file line by line and print

  • 0

In java i would like to read a file line by line and print the line to the output.
I want to solve this with regular expressions.

while (...)
{
  private static java.util.regex.Pattern line = java.util.regex.Pattern.compile(".*\\n");
  System.out.print(scanner.next(line));
}

The regex in the code is not correct, as i get InputMismatchException.
I am working on this regex for 2 hours. Please help with it.

With regex powertoy i see that “.*\n” is correct. But my program runs incorrectly.

The whole source is:

/**
 * Extracts the points in the standard input in off file format to the standard output in ascii points format.
 */

 import java.util.regex.Pattern;
 import java.util.Scanner;

class off_to_ascii_points 
{
    private static Scanner scanner = new Scanner(System.in);    
    private static Pattern fat_word_pattern = Pattern.compile("\\s*\\S*\\s*");
    private static Pattern line = Pattern.compile(".*\\n", Pattern.MULTILINE);

    public static void main(String[] args) 
    {
        try
        {
            scanner.useLocale(java.util.Locale.US);

                    /* skip to the number of points */
            scanner.skip(fat_word_pattern);

            int n_points = scanner.nextInt();

                    /* skip the rest of the 2. line */
            scanner.skip(fat_word_pattern); scanner.skip(fat_word_pattern);

            for (int i = 0; i < n_points; ++i)
            {
                    System.out.print(scanner.next(line));
                      /*
                      Here my mistake is. 
                      next() reads only until the delimiter, 
                      which is by default any white-space-sequence. 
                      That is next() does not read till the end of the line 
                      what i wanted.

                      Changing "next(line)" to "nextLine()" solves the problem.
                      Also, setting the delimiter to line_separator 
                      right before the loop solves the problem too.
                      */
            }

        }
        catch(java.lang.Exception e)
        {
            System.err.println("exception");
            e.printStackTrace();
        }
    }
}

The beginning of an example input is:

OFF
4999996 10000000 0
-28.6663 -11.3788 -58.8252 
-28.5917 -11.329 -58.8287 
-28.5103 -11.4786 -58.8651 
-28.8888 -11.7784 -58.9071 
-29.6105 -11.2297 -58.6101 
-29.1189 -11.429 -58.7828 
-29.4967 -11.7289 -58.787 
-29.1581 -11.8285 -58.8766 
-30.0735 -11.6798 -58.5941 
-29.9395 -11.2302 -58.4986 
-29.7318 -11.5794 -58.6753 
-29.0862 -11.1293 -58.7048 
-30.2359 -11.6801 -58.5331 
-30.2021 -11.3805 -58.4527 
-30.3594 -11.3808 -58.3798 

I first skip to the number 4999996 which is the number of lines containing point coordinates. These lines are that i am trying to write to the output.

  • 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-05-11T22:12:40+00:00Added an answer on May 11, 2026 at 10:12 pm

    I suggest using

    private static Pattern line = Pattern.compile(".*");
    
    scanner.useDelimiter("[\\r\\n]+"); // Insert right before the for-loop
    
    System.out.println(scanner.next(line)); //Replace print with println
    

    Why your code doesn’t work as expected:

    This has to do with the Scanner class you use and how that class works.

    The javadoc states:

    A Scanner breaks its input into tokens
    using a delimiter pattern, which by
    default matches whitespace.

    That means when you call one of the Scanner’s.next* methods the scanner reads the specified input until the next delimiter is encountered.

    So your first call to scanner.next(line) starts reading the following line

    -28.6663 -11.3788 -58.8252 
    

    And stops at the space after -28.6663. Then it checks if the token (-28.6663) matches your provided pattern (.*\n) which obviously doesn’t match (-28.6663). That’s why.

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

Sidebar

Ask A Question

Stats

  • Questions 124k
  • Answers 124k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Are you running on OS 3.0? I saw the same… May 12, 2026 at 1:19 am
  • Editorial Team
    Editorial Team added an answer It looks like you need to register Apache::Session::Memcached with Apache::Session::Wrapper,… May 12, 2026 at 1:19 am
  • Editorial Team
    Editorial Team added an answer Use DATENAME or DATEPART: SELECT DATENAME(dw,GETDATE()) -- Friday SELECT DATEPART(dw,GETDATE())… May 12, 2026 at 1:19 am

Related Questions

What language should I use for file and string manipulation? This might seem objective,
How can I have a text file (or XML file) represented as a whole
I challenge you :) I have a process that someone already implemented. I will
I need to store some simple properties in a file and access them from

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.