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

  • Home
  • SEARCH
  • 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 773783
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T19:01:16+00:00 2026-05-14T19:01:16+00:00

Problem: I have to design an algorithm, which does the following for me: Say

  • 0

Problem:

I have to design an algorithm, which does the following for me:

Say that I have a line (e.g.)

alert tcp 192.168.1.1 (caret is currently here)

The algorithm should process this line, and return a value of 4.

I coded something for it, I know it’s sloppy, but it works, partly.

private int counter = 0;
    public void determineRuleActionRegion(String str, int index) {
        if (str.length() == 0 || str.indexOf(" ") == -1) {
            triggerSuggestionList(1);
            return;
        }

        //remove duplicate space, spaces in front and back before searching
        int num = str.trim().replaceAll(" +", " ").indexOf(" ", index);
        //Check for occurances of spaces, recursively
        if (num == -1) { //if there is no space
            //no need to check if it's 0 times it will assign to 1
            triggerSuggestionList(counter + 1);
            counter = 0;
            return; //set to rule action
        } else { //there is a space
            counter++;
            determineRuleActionRegion(str, num + 1);
        }

    } //end of determineactionRegion()

So basically I find for the space and determine the region (number of words typed). However, I want it to change upon the user pressing space bar <space character>.

How may I go around with the current code?

Or better yet, how would one suggest me to do it the correct way? I’m figuring out on BreakIterator for this case…

To add to that, I believe my algorithm won’t work for multi sentences. How should I address this problem as well.

—

The source of String str is acquired from textPane.getText(0, pos + 1);, the JTextPane.

Thanks in advance. Do let me know if my question is still not specific enough.

—

More examples:

alert tcp $EXTERNAL_NET any -> $HOME_NET 22 <caret>

return -1 (maximum of the typed text is 7 words)

alert tcp 192.168.1.1 any<caret> 

return 4 (as it is still at 2nd arg)

alert tcp<caret>

return 2 (as it is still at 2nd arg)

alert tcp <caret>

return 3

alert tcp $EXTERNAL_NET any -> <caret>

return 6

It is something like shell commands. As above. Though I think it does not differ much I believe, I just want to know how many arguments are typed. Thanks.

—

Pseudocode

Get whole paragraph from textpane
  if more than 1 line -> process the last line
      count how many arguments typed and return appropriate number
  else
    process current line
      count how many arguments typed and return appropriate number
End
  • 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-14T19:01:17+00:00Added an answer on May 14, 2026 at 7:01 pm

    The codes provided by polygenelubricants and helios work, to a certain extent. It addresses the aforementioned problem I’d stated, but not with multi-lines. helios’s code is more straightforward.

    However both codes did not address the problem when you press enter in the JTextPane, it will still return back the old count instead of 1 as the split() returns it as one sentence instead of two.

    E.g. alert tcp <enter is pressed>
    By right it should return 1 since it is a new sentence. It returned 2 for both algorithms.
    Also, if I highlight all and delete both algorithms will throw NullPointerException as there is no string to be split.

    I added one line, and it solved the problems mentioned above:

    public void determineRuleActionRegion(String str) {
        //remove repetitive spaces and concat $ for new line indicator
        str = str.trim().replaceAll(" +", " ") + "$";
        String[] lines = str.split("\r?\n|\r");
        String lastLine = lines[lines.length - 1];
        String[] tokens = lastLine.split("\\s+", -1);
        int pos = (tokens.length <= 7) ? tokens.length : -1;
        triggerSuggestionList(pos);
        System.out.println("Current pos: " + pos);
        return;
    } //end of determineactionRegion()
    

    With that, when split() parses the str, the "$" will create another line, which will be the last line regardless, and the count now will return to one. Also, there will not be NullPointerException as the "$" is always there.

    However, without the help of polygenelubricants and helios, I don’t think I will be able to figure it out so soon. Thanks guys!

    EDIT: Okay… apparently split("\r?\n|\r",-1) works the same. Question is should I accept polygenelubricants or my own? Hmm.

    2nd EDIT: One thing bad about concatenating '%' to the end of the str, lastLine.endsWith(" ") == true will return false. So have to use split("\r?\n|\r",-1) and lastLine.endsWith(" ") == true for the complete solution.

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

Sidebar

Ask A Question

Stats

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

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

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

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

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer In addition to Jason's link, you should check out these… May 15, 2026 at 11:00 pm
  • Editorial Team
    Editorial Team added an answer Put a trigger in your template that will replace the… May 15, 2026 at 11:00 pm
  • Editorial Team
    Editorial Team added an answer SDL_Color sdlColors[] = {normColor, redColor, blackColor..............}; May 15, 2026 at 11:00 pm

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.