Count characters in a code

Reply

Join Date: Oct 2009
Posts: 2
Reputation: chengeto is an unknown quantity at this point 
Solved Threads: 0
chengeto chengeto is offline Offline
Newbie Poster

Count characters in a code

 
0
  #1
Oct 21st, 2009
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?

  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
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 43
Reputation: .11 is an unknown quantity at this point 
Solved Threads: 7
.11 .11 is offline Offline
Light Poster
 
0
  #2
Oct 21st, 2009
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:

  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.


  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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 2
Reputation: chengeto is an unknown quantity at this point 
Solved Threads: 0
chengeto chengeto is offline Offline
Newbie Poster
 
0
  #3
Oct 21st, 2009
Originally Posted by .11 View Post
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 ?
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 43
Reputation: .11 is an unknown quantity at this point 
Solved Threads: 7
.11 .11 is offline Offline
Light Poster
 
0
  #4
Oct 21st, 2009
Originally Posted by chengeto View Post
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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 19
Reputation: nola_Coder is an unknown quantity at this point 
Solved Threads: 0
nola_Coder nola_Coder is offline Offline
Newbie Poster
 
0
  #5
Oct 21st, 2009
try using this method to check each character:

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


don't forget, you'll need to import java.lang.Character;
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,610
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 465
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human
 
0
  #6
Oct 21st, 2009
> 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.
I don't accept change; I don't deserve to live.
Reply With Quote Quick reply to this message  
Reply

Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC