| | |
Count characters in a code
![]() |
•
•
Join Date: Oct 2009
Posts: 2
Reputation:
Solved Threads: 0
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)
import javax.swing.*; publiemc class ExamPractise { public static void main (String [] geek) { String sentence = JOptionPane.showInputDialog("Enter a any sentence"); int countCharacters=0; for (int i = 0; i < sentence.length(); i++) { char c = sentence.charAt(i); if (c !=' '||c!='?'||c!='.'||c!=' ') { countCharacters++; } } System.out.println("There are" + " " + countCharacters + " " + "word"); } }
Last edited by chengeto; Oct 21st, 2009 at 1:25 am. Reason: Typo
•
•
Join Date: Feb 2009
Posts: 43
Reputation:
Solved Threads: 7
0
#2 Oct 21st, 2009
publiemc class ExamPractiseMake 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)
if ((c >= 65 && c > 91)) { countCharacters++; }
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)
if ((c >= 65 && c > 91) || (lower case condition) || (numbers condition)) { countCharacters++; }
Last edited by .11; Oct 21st, 2009 at 1:45 am.
•
•
Join Date: Oct 2009
Posts: 2
Reputation:
Solved Threads: 0
0
#3 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.
•
•
Join Date: Feb 2009
Posts: 43
Reputation:
Solved Threads: 7
0
#4 Oct 21st, 2009
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.
![]() |
Similar Threads
- RE help, matching strings ending with ";" (Python)
- Word Count Help !! (Visual Basic 4 / 5 / 6)
- Script to list dir path and count characters? (Shell Scripting)
- count characters in a string (Perl)
- RE: word count programming for python (wc) (Python)
- Count characters of a string. (C++)
- Frequency of characters entered. (C)
- Character/letter count (Pascal and Delphi)
- Please Help - Should be easy. (C++)
Other Threads in the Java Forum
- Previous Thread: Help!!
- Next Thread: extends problem
| Thread Tools | Search this Thread |
account android api applet application array arrays automation awt bidirectional binary birt bluetooth busy_handler(null) chat class classes client code columns component database designadrawingapplicationusingjavajslider draw eclipse error errors eventlistener exception expand fractal game givemetehcodez graphics gui guidancer homework html ide image inetaddress inheritance integer intellij j2me java javamicroeditionuseofmotionsensor javaprojects jlabel jme jni jpanel jtextfield julia linux list loop map method methods midlethttpconnection mobile mobiledevelopmentcreatejar monitoring myaggfun netbeans newbie nullpointerexception open-source plazmic print problem program programming project property recursion ria scanner search server set smart sms smsspam sort sourcelabs splash sql sqlite static string subclass support swing testautomation threads tree webservices windows






