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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T13:36:10+00:00 2026-06-13T13:36:10+00:00

I am attempting to extract words as strings out of one very long string

  • 0

I am attempting to extract words as strings out of one very long string that contains many words. I’m trying to take this big blob of a string, move through it with a for loop, building strings as I go, adding them to the hashset, and keeping track of how many words I have in the process. I was so proud out myself when I had everything built and eclipse wasn’t showing any obvious errors, then when I went to test it I had a 0 counter, 0 hashset.size and an empty hashset 🙁

Here is the code I’ve been fudging with:

    public int countUniqueWords(String line) {
    hashset = new HashSet<String>();
    word = new StringBuilder();
    int endOfLine = line.length() - 1;
    boolean isWord = false;
    String stringWord = null;
    Integer counter = 0;

    for (int i = 0; i < line.length(); i++) {
        if (Character.isLetter(line.charAt(i)) == true && i != endOfLine) {
            word.append(line.charAt(i));
        } else if (Character.isLetter(line.charAt(i)) == false && isWord == true) {
            counter++;
            stringWord = word.toString();
            hashset.add(stringWord);
            word = null;
            isWord = false;
        } else if (Character.isLetter(line.charAt(i)) && i == endOfLine) {
            counter++;
            stringWord = word.toString();
            hashset.add(stringWord);
        }
    }
    System.out.println(counter);
    System.out.println(hashset.size());
    System.out.println(hashset);
    return counter;
}

I will continue to search and kick the tires. In the meantime, if anyone has any suggestions I will reward you with lots of mental good vibes over here in my little home office. It seems like there are at least one or two very fundamental errors I am making here, because obviously it’s not even iterating through the loop like it’s supposed to. I suspect it has something to do with me using StringBuilder together with HashSets and the Character class, all mashed together while not understanding any of these particularly well. And yes, I’ve poured over the oracle docs.

  • 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-13T13:36:11+00:00Added an answer on June 13, 2026 at 1:36 pm

    The first condition is true for all characters

       if (Character.isLetter(line.charAt(i)) == true && i != endOfLine)
    

    The second condition is always false as isWord is false, the statements under condition will never execute

       else if (Character.isLetter(line.charAt(i)) == false && isWord == true) 
    

    The third condition will never execute as this is same as first condition

        else if (Character.isLetter(line.charAt(i)) && i == endOfLine)
    

    You need to change the conditions by turning on/off the ‘isWord’ flag when required.

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

Sidebar

Related Questions

I've spent the past day attempting to extract a one XML node out of
I am attempting to extract Weibull distribution parameters (shape 'k' and scale 'lambda') that
I am attempting to extract all instances of a particular format from a string:
I just found a weird behavior when attempting to extract a string from the
I'm attempting to extract not numeric values from a matrix like this: 32540_at 0.138306
So I have about 4,000 word docs that I'm attempting to extract the text
I'm attempting to extract urls from a string, they aren't standardized so some are
I am attempting to extract all data from a file with one regex expression.
I'm playing around with Expressjs and am attempting to extract the page title from
Attempting to use the data series from this example no longer passes the JSONLint

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.