944,192 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 1029
  • Java RSS
Oct 21st, 2009
0

Count characters in a code

Expand Post »
I am trying to get this program to count the number of characters in a string. By number of characters l mean not punctuation or a space.What is wrong with my code because l don't understand why the program won't work?

Java Syntax (Toggle Plain Text)
  1. import javax.swing.*;
  2.  
  3. publiemc class ExamPractise
  4. {
  5. public static void main (String [] geek)
  6.  
  7. {
  8. String sentence = JOptionPane.showInputDialog("Enter a any sentence");
  9.  
  10.  
  11. int countCharacters=0;
  12.  
  13.  
  14.  
  15. for (int i = 0; i < sentence.length(); i++)
  16. {
  17. char c = sentence.charAt(i);
  18.  
  19.  
  20. if (c !=' '||c!='?'||c!='.'||c!=' ')
  21. {
  22. countCharacters++;
  23. }
  24. }
  25.  
  26.  
  27. System.out.println("There are" + " " + countCharacters + " " + "word");
  28.  
  29.  
  30. }
  31. }
Last edited by chengeto; Oct 21st, 2009 at 1:25 am. Reason: Typo
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
chengeto is offline Offline
2 posts
since Oct 2009
Oct 21st, 2009
0
Re: Count characters in a code
publiemc class ExamPractise

Make it public

And:

if (c !=' '||c!='?'||c!='.'||c!=')
{
       countCharacters++;
}

^What do you wanna c not to be there?


Should be find after that.
-----

And I checked out the program, it still counts for spaces and punctuation.



EDIT AGAIN

Since your counting the characters you should make you if statement to check the ASCII value for an example:

Java Syntax (Toggle Plain Text)
  1. if ((c >= 65 && c > 91))
  2. {
  3. countCharacters++;
  4. }

That will check for uppercase letters only. Heres an ascii table.
http://web.cs.mun.ca/~michael/c/ascii-table.html

Just OR it with the other conditionals you make i.e.


Java Syntax (Toggle Plain Text)
  1. if ((c >= 65 && c > 91) || (lower case condition) || (numbers condition))
  2. {
  3. countCharacters++;
  4. }
Last edited by .11; Oct 21st, 2009 at 1:45 am.
.11
Reputation Points: 10
Solved Threads: 7
Light Poster
.11 is offline Offline
49 posts
since Feb 2009
Oct 21st, 2009
0
Re: Count characters in a code
Click to Expand / Collapse  Quote originally posted by .11 ...
publiemc class ExamPractise

Make it public

And:

if (c !=' '||c!='?'||c!='.'||c!=')
{
       countCharacters++;
}

^What do you wanna c not to be there?


Should be find after that.
-----

And I checked out the program, it still counts for spaces and punctuation.
Right now if l enter the word Obama Obama ? it counts the question mark and space between the two words as a character and l don't want it to do that.I don't know how do l get around that ?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
chengeto is offline Offline
2 posts
since Oct 2009
Oct 21st, 2009
0
Re: Count characters in a code
Click to Expand / Collapse  Quote originally posted by chengeto ...
Right now if l enter the word Obama Obama ? it counts the question mark and space between the two words as a character and l don't want it to do that.I don't know how do l get around that ?
Check out the ASCII table above I edit my post, the upper case condition is checked for, just check for the other two. And it will eliminate the characters that aren't a letter and number.
.11
Reputation Points: 10
Solved Threads: 7
Light Poster
.11 is offline Offline
49 posts
since Feb 2009
Oct 21st, 2009
0
Re: Count characters in a code
try using this method to check each character:

if (Character.isLetter(c)) {
countCharacters++;
}


don't forget, you'll need to import java.lang.Character;
Reputation Points: 10
Solved Threads: 0
Light Poster
nola_Coder is offline Offline
49 posts
since Oct 2009
Oct 21st, 2009
0
Re: Count characters in a code
> By number of characters l mean not punctuation or a space

Look into the isLetter method of the Character class; modify your logic to increment the count only if the above method returns true .
Last edited by ~s.o.s~; Oct 21st, 2009 at 1:56 am.
Super Moderator
Featured Poster
Reputation Points: 3241
Solved Threads: 719
Failure as a human
~s.o.s~ is offline Offline
8,873 posts
since Jun 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Help!!
Next Thread in Java Forum Timeline: extends problem





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC