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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T10:15:26+00:00 2026-05-27T10:15:26+00:00

I believe I am not using correctly String Tokenizer. Here is my code: buffer

  • 0

I believe I am not using correctly String Tokenizer. Here is my code:

  buffer = new byte[(int) (end - begin)];
                fin.seek(begin);
                fin.read(buffer, 0, (int) (end - begin));

                StringTokenizer strk = new StringTokenizer(new String(buffer),
                                DELIMS,true);

As you can see I am reading a chunk of lines from a file(end and begin are line numbers) and I am transfering the data to a string tokenizer. My delimitators are:

DELIMS = "\r\n ";

because I want to separate words that have a space between them, or are on the next line.
However this code sometimes separates whole words also. What could be the explanation?? Is my DELIMS string conceived wrong?

Also I am passing “true” as an argument to the tokenizer because I want the delimitators to be treated as tokens as well.( I want this because I want to count the line I am currently at)

Could you please help me. Thanks a lot.

  • 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-27T10:15:27+00:00Added an answer on May 27, 2026 at 10:15 am

    You could always wrap your input stream in a LineNumberReader. That will keep track of the line number for you. LineNumberReader extends BufferedReader, which has a readLine() method. With that, you could use a regular StringTokenizer to get your words as tokens. You could use regular expressions or Scanner, but for this case, StringTokenizer is simpler for beginners to understand and quicker.

    You must have a RandomAccessFile. You didn’t specify that, but I’m guessing based on the methods you used. Try something like:

    byte [] buffer = ...; // you know how to get this.
    ByteArrayInputStream stream = new ByteArrayInputStream(buffer);
    
    // if you have java.util.Scanner
    {
        int lineNumber = 0;
        Scanner s = new Scanner(stream);
    
        while (s.hasNextLine()) {
            lineNum++;
            String line = s.nextLine();
            System.out.format("I am on line %s%n", lineNum);
            Scanner lineScanner = new Scanner(line);
    
            while (lineScanner.hasNext()) {
                String word = lineScanner.next();
                // do whatever with word
            }
        }
    }
    
    // if you don't have java.util.Scanner, or want to use StringTokenizer
    {
        LineNumberReader reader = new LineNumberReader(
                              new InputStreamReader(stream));
        String line = null;
    
        while ((line = reader.nextLine()) != null) {
            System.out.println("I am on line " + reader.getLineNumber());
            StringTokenizer tok = new StringTokenizer(line);
    
            while (tok.hasMoreTokens()) {
                String word = tok.nextToken();
                // do whatever with word
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

First of all, I do not believe this belongs on Superuser. This belongs here
I am looking at improving a package that I believe not to be threadsafe
Should links on a web page ALWAYS be underlined? I do not believe this
I'm working on a web applications where - believe it or not- the users
I believe several of us have already worked on a project where not only
I believe it is related to CORBA in some way (I'm not sure). I'm
Not used set_intersection before, but I believe it will work with maps. I wrote
I believe there is a way to do this, but I'm not familiar with
I believe I already know the answer, but I am not 100% sure, so
I have been told and I'm not sure I believe this: Removing white space

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.