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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T15:33:16+00:00 2026-05-29T15:33:16+00:00

I have a scanner set up that is working on an InputStream. I am

  • 0

I have a scanner set up that is working on an InputStream.

I am using Scanner.nextLine() to advance to each line, then doing some regular expression work on each line.

I have a regular expression that is basically like [\w\p{Z}]+?[;\n\r] to pick up anything to the end of that line, or just ONE thing, if they are semi-colon delimited.

so if my InpustStream looks like

abcd;
xyz

It will pick up abcd;, but not xyz.

I think this is because scanner is consuming the newline character at the end of the line of text must be getting consumed somehow when the .nextLine() function is being called. Can anyone tell me how to fix this problem?

As an additional point of info, for my regex, i am compiling the pattern with Pattern.DOTALL

Thanks!

  • 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-29T15:33:16+00:00Added an answer on May 29, 2026 at 3:33 pm

    Actually, you’re the one that’s causing the problem, by trying to consume a newline at the end of the last line. :-/ It’s perfectly valid for the last line to end abruptly without a newline character, but your regex requires it to have one. You might be able to fix that by replacing the newline with an anchor or a lookahead, but there are much easier ways to go about this.

    One is to override the default delimiter and iterate over the fields with next():

    Scanner sc1 = new Scanner("abcd;\nxyz");
    sc1.useDelimiter("[;\r\n]+");
    while (sc1.hasNext())
    {
      System.out.printf("%s%n", sc1.next());
    }
    

    The other is to iterate over the lines with nextLine() (using the default delimiter) and then split each line on semicolons:

    Scanner sc2 = new Scanner("abcd;\nxyz");
    while (sc2.hasNextLine())
    for (String item : sc2.nextLine().split(";"))
    {
      System.out.printf("%s%n", item);
    }
    

    Scanner’s API is one of the most bloated and unintuitive I’ve ever worked with, but you can greatly reduce the pain of using it if you remember these two crucial points:

    1. Think in terms of matching the delimiters, not the fields (like you do with String’s split()).
    2. Never call one of the nextXXX() methods without first calling the corresponding hasNextXXX() method.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using Java's Scanner to parse some text. Say I have set as
I have been given two different Microsoft Word document that my virus scanner has
I have been trying to write a bare-bones ping scanner using Perl for internal
So I have a set of data that includes a column of dates and
I'm updating some old old code (that works for now, but is using OpenTransport,
I have written some code in java that will guess a number based on
I have been working on this problem for quite some time and can't figure
I have a C# (WinForm) program that supports scanning using WIA. I am trying
I'm working on an application that calls the ZXing scanner on button click. That
I have been trying to make a clock that the user can set. I

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.