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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T21:13:33+00:00 2026-05-27T21:13:33+00:00

I made a simple parser with java that reads a file one character at

  • 0

I made a simple parser with java that reads a file one character at a time and constructs words.

I tried to run it under Linux and I noticed that looking for '\n' doesn’t work. Although if I compare the character with the value 10 it works as expected. According to the ASCII table value 10 is LF (line feed). I read somewhere (I don’t remember where) that Java should be able to find a newline only by looking for '\n'.

I am using BufferedReader and the read method to read characters.

EDIT

readLine cannot be used because it will produce other problems

It looks like the problem appears when I am using files with mac/windows file endings under linux.

  • 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-27T21:13:33+00:00Added an answer on May 27, 2026 at 9:13 pm

    here are two ways can do it

    1- use read line by line and split each using a regular expression to get the single words

    2- write your own isDelimiter method and use it to check whether you reached a split contition or not

    package misctests;
    
    import static org.junit.Assert.assertEquals;
    import static org.junit.Assert.assertNotNull;
    import java.util.ArrayList;
    import java.util.List;
    import org.junit.Test;
    
    
    public class SplitToWords {
    
        String someWords = "Lorem ipsum\r\n(dolor@sit)amet,\nconsetetur!\rsadipscing'elitr;sed~diam";
        String delimsRegEx = "[\\s;,\\(\\)!'@~]+";
        String delimsPlain = ";,()!'@~"; // without whitespaces
    
        String[] expectedWords = {
            "Lorem",
            "ipsum",
            "dolor",
            "sit",
            "amet",
            "consetetur",
            "sadipscing",
            "elitr",
            "sed",
            "diam"
        };
    
        private static final class StringReader {
            String input = null;
            int pos = 0;
            int len = 0;
            StringReader(String input) {
                this.input = input == null ? "" : input;
                len = this.input.length();
            }
    
            public boolean hasMoreChars() {
                return pos < len;
            }
    
            public int read() {
                return hasMoreChars() ? ((int) input.charAt(pos++)) : 0;
            }
        }
    
        @Test
        public void splitToWords_1() {
            String[] actual = someWords.split(delimsRegEx);
            assertEqualsWords(expectedWords, actual);
        }
    
        @Test
        public void splitToWords_2() {
            StringReader sr = new StringReader(someWords);
            List<String> words = new ArrayList<String>();
            StringBuilder sb = null;
            int c = 0;
            while(sr.hasMoreChars()) {
                c = sr.read();
                while(sr.hasMoreChars() && isDelimiter(c)) {
                    c = sr.read();
                }
                sb = new StringBuilder();
                while(sr.hasMoreChars() && ! isDelimiter(c)) {
                    sb.append((char)c);
                    c = sr.read();
                }
                if(! isDelimiter(c)) {
                    sb.append((char)c);
                }
                words.add(sb.toString());
            }
    
            String[] actual = new String[words.size()];
            words.toArray(actual);
    
            assertEqualsWords(expectedWords, actual);
        }
    
        private boolean isDelimiter(int c) {
            return (Character.isWhitespace(c) ||
                delimsPlain.contains(new String(""+(char)c))); // this part is subject for optimization
        }
    
        private void assertEqualsWords(String[] expected, String[] actual) {
            assertNotNull(expected);
            assertNotNull(actual);
            assertEquals(expected.length, actual.length);
            for(int i = 0; i < expected.length; i++) {
                assertEquals(expected[i], actual[i]);
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to run a java class that reads off some values from a
We made a simple application and using GoogleAppEngineLauncher (GAEL) ran that locally. Then we
For an assignment I've made a simple C++ program that uses a superclass (Student)
I have made my own file type (.ddd) and I made a simple program
I've made a small tool that parses a chunk of text, does some simple
Hi I wrote a java code to find longest word made of other words.
On Windows, testing different OSes is made simple using VMs. Is there a simple
I made a simple counter, but it increments by 2 instead of 1. $handle
I've made a simple http server using Twisted, which sends the Content-Type: multipart/x-mixed-replace header.
I have made a simple test application for the issue, two winforms each containing

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.