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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T01:22:43+00:00 2026-06-12T01:22:43+00:00

I am working on a program that will count words that is typed or

  • 0

I am working on a program that will count words that is typed or pasted in the text area. It counts the words correctly if it doesn’t have a double space. I use the split method for this and uses the for loop for objects to count the words.

here is the simplest form of the part of the code that got some problem…


 public static void main(String[] args) {
    String string = "Java  C++ C#";
    String[] str;
    int c=0;
    str = string.split(" ");
    for(String s:str){
        if(s.equals(" "))
            System.out.println("skipped space");
        else{
            System.out.println("--"+s);
            c++;
        }
    }
    System.out.println("words; " + c);
}

im trying to check if the string contained in the object s is a space but how I do it didn’t work.

I want it to output like this

--Java
skipped space
--C++
--C#

words; 3

but the result is

--Java
--
--C++
--C#
words; 4

Any Suggestions on how can i solve this? or which part i got a problem?
thanks in advance.

  • 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-12T01:22:44+00:00Added an answer on June 12, 2026 at 1:22 am

    split expects a regular expression. Use it’s power.

    str = string.split(" +");
    
    //more sophisticated
    str = string.split("\\s+");
    
    • \s matches any whitespace (not just space, but tabs. newline etc.)
    • + means “one or more of them”
    • The first backslash is needed to escape the second to remove the special meaning inside the string
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working a program that will have a bunch of threads processing data. Each
I'm working on a program that will sort files based on extension I currently
I'm working on a program that will need to delete a folder (and then
I'm working on a Google App Engine program that will require some basic spell
Working on a small program that will take a string from a user and
I'm working on a program that will tell what level a programmer is at
I'm working on a VB.NET program that will automatically backup my work to my
for an assignment I have to write a program that will take in an
I am working, using OpenCV/C++ framework, on a program that basically count elliptical objects
For class I have an assignment: Write a C++ program that will output the

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.