3 Replies - 11384 Views - Last Post: 12 March 2010 - 04:08 PM Rate Topic: -----

#1 tnamdak  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 16
  • Joined: 22-February 10

Empty character literal error

Posted 11 March 2010 - 10:45 PM

How do fix an error that display empty character literal and unclosed character literal

  for (int i = 0; i <= length -1; i++) {
            ch = phrase.charAt(i);
         if (ch == ('')); // this is where my problem is 
         {
         countBlank++;
        }
    }



Is This A Good Question/Topic? 0
  • +

Replies To: Empty character literal error

#2 javabie  Icon User is offline

  • D.I.C Head

Reputation: 36
  • View blog
  • Posts: 117
  • Joined: 12-February 10

Re: Empty character literal error

Posted 11 March 2010 - 10:53 PM

if (ch == '\0')  // <= note that \0 is empty character and also i removed the ';' at the end of the if statement.
{

}



but if you want to count word in a string then you should compare it with space character...
if (ch == ' ') // there is a space between ' '
{

}


This post has been edited by javabie: 11 March 2010 - 10:55 PM

Was This Post Helpful? 0
  • +
  • -

#3 zim1985  Icon User is offline

  • Grand Inquisitor
  • member icon

Reputation: 74
  • View blog
  • Posts: 557
  • Joined: 19-February 10

Re: Empty character literal error

Posted 11 March 2010 - 11:12 PM

sAlso, if you wanted to count the words in a String...
String word = "Hola mi amigo!  Como estas ahora?";
int wordCount = 0;

for(int i = 0; i < word.length(); i++)
{
  if(word.charAt(i) == ' ')
  {
    wordCount++;
  }
}



I reread the first post, my bad...

This post has been edited by zim1985: 11 March 2010 - 11:13 PM

Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10196
  • View blog
  • Posts: 37,655
  • Joined: 27-December 08

Re: Empty character literal error

Posted 12 March 2010 - 04:08 PM

You can do it in a single statement using either the String split() method, which returns a String[] from a single String object, splitting based upon a given delimiter. You can also create a StringTokenizer object, which is very similar to split(); except that it returns one element at a time as you iterate through it vs. an entire array at once. So:
int numSpaces = stringName.split(" ").length;
//OR
int spaces = new StringTokenizer(stringName).countTokens(); 


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1