| | |
Making a program that counts the # of characters
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Oct 2007
Posts: 89
Reputation:
Solved Threads: 0
the below program is supposed to read the string you input and count the amount of times that the letter 'a' appears in it. if it doesn't find the letter 'a', it's supposed to tell you that there aren't enough arguments (which i may change to say that "there aren't any 'a' characters or something) but i'm stuck and i don't know exactly what else to do.
Java Syntax (Toggle Plain Text)
//Page 297 Exercise 8.4 //Saying program found x # of this character in string. public class Test { //variables char a = a; { public static void main(String[] args) { if (args.length != 2) { System.out.println("Not enough arguments"); } else { Count(args[0], args[1].charAt(0)); } } public static int Count(String str, char a) { int charCount = 0; System.out.print(str); System.out.print(a); for (int i = 0; i<str.length(); i++) { if (str.charAt(i) == a) { charCount ++; } return charCount; } } } }
What exactly is the problem? Other than the fact, of course, that the method you're calling returns a value and you are simply ignoring that value, rather than storing it in a variable, evaluating it, then printing either it or the message.
Java Programmer and Sun Systems Administrator
----------------------------------------------
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
----------------------------------------------
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
•
•
Join Date: Oct 2007
Posts: 89
Reputation:
Solved Threads: 0
now i'm working with the below code that's compiling and going 'sort-of' where i want but the number that it is giving for the amount of times 'a' shows up is strange like 000000111
Help?
Help?
Java Syntax (Toggle Plain Text)
public class Test { //variables char a = 'a'; public static void main(String[] args) { if (args.length < 1) { System.out.println("Not enough arguments"); } else { Count(args[0], args[1].charAt(0)); } } public static int Count(String str, char a) { int charCount = 0; System.out.print("The number of character 'a' found is: "); for (int i = 0; i<str.length(); i++) { if (str.charAt(i) == a) { charCount ++; } System.out.print(charCount); } return charCount; } }
why are you printing the count inside the loop?
why are you not doing anything with the returnvalue of the method?
and don't use Eclipse or any other IDE until you know the language. Those things slow down your learning by making you learn the tool and masking your own misunderstanding in places.
why are you not doing anything with the returnvalue of the method?
and don't use Eclipse or any other IDE until you know the language. Those things slow down your learning by making you learn the tool and masking your own misunderstanding in places.
As people are clearly allowed to attack me but I'm not allowed to defend myself, I no longer post to this site.
![]() |
Similar Threads
- Word Count Help !! (Visual Basic 4 / 5 / 6)
- Counting all Non-blank Characters in a String (Java)
- what is the simplest way to output my results (C)
- Counting specific characters in a string (C)
- function to count vowels (C++)
- Pointer logic advice (C)
- Want help making program access a web page automatic. (C)
Other Threads in the Java Forum
- Previous Thread: I can't understand what's wrong
- Next Thread: help with java
Views: 1875 | Replies: 15
| Thread Tools | Search this Thread |
Tag cloud for Java
3d @param affinetransform android api apple applet application arc arguments array arrays automation binary bluetooth byte c# chat class classes click client code compare component corrupted database detection draw eclipse error event exception file fractal game givemetehcodez graphics gui guitesting helpwithhomework html ide image input integer j2me java java.xls javaprojects jmf jni jpanel julia keytool linux list loop map method methods mobile netbeans newbie number object oracle os pong print problem producer program programming project projectideas read recursion reflection replaysolutions rim scanner screen server set size sms socket sort sql string swing terminal test threads time transfer tree web windows






