| | |
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: 36
Reputation:
Solved Threads: 6
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: 36
Reputation:
Solved Threads: 6
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 |
911 addball addressbook android api append applet application apps array arrays automation binary bluetooth businessintelligence button card character class client code collision component crashcourse css csv database eclipse ee error fractal free game gis givemetehcodez graphics gui html ide image integer integration j2me japplet java javaarraylist javadoc javafx javaprojects jni jpanel julia jvm linux list loan machine map method methods migrate mobile netbeans newbie objects oriented output panel phone physics problem program programming project projects radio recursion replaydirector reporting researchinmotion scanner se server service set sms software sort sql string swing test textfield threads transfer tree trolltech ubuntu utility windows






