3 Replies - 2139 Views - Last Post: 30 September 2012 - 03:47 AM Rate Topic: -----

#1 kamirusen   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 29-November 11

counting words in string - problem in skipping double space

Posted 30 September 2012 - 01:45 AM

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#"; //i put double space after the word Java
        String[] str;
        int c=0;
        str = string.split(" ");
        for(String s:str){
            if(s==" ")
                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.

This post has been edited by kamirusen: 30 September 2012 - 01:48 AM


Is This A Good Question/Topic? 0
  • +

Replies To: counting words in string - problem in skipping double space

#2 Vinski   User is offline

  • New D.I.C Head

Reputation: 9
  • View blog
  • Posts: 19
  • Joined: 29-September 12

Re: counting words in string - problem in skipping double space

Posted 30 September 2012 - 02:02 AM

When you compare a string, you shouldn't use equals sign. You should replace if(s==" ") with if(s.equals(" ")) or if(s.equalsIgnoreCase(" ")).
Was This Post Helpful? 0
  • +
  • -

#3 kamirusen   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 29-November 11

Re: counting words in string - problem in skipping double space

Posted 30 September 2012 - 02:10 AM

just the same result .... it still don't enter the if statement when there was a space stored in the string
Was This Post Helpful? 0
  • +
  • -

#4 CasiOo   User is offline

  • D.I.C Lover
  • member icon

Reputation: 1578
  • View blog
  • Posts: 3,551
  • Joined: 05-April 11

Re: counting words in string - problem in skipping double space

Posted 30 September 2012 - 03:47 AM

Remember that split(" ") will split on every space.
You won't have any tokens that are equal to " " instead they will be empty if there are multiple spaces

public class Snippet {
	public static void main(String[] args) {
	        String string = "Java  C++ C#"; //i put double space after the word Java
	        int c=0;
	        String[] 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);
	    }
}


Was This Post Helpful? 2
  • +
  • -

Page 1 of 1