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: leroi green is an unknown quantity at this point 
Solved Threads: 0
leroi green leroi green is offline Offline
Junior Poster in Training

Making a program that counts the # of characters

 
0
  #1
Nov 8th, 2007
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.


  1. //Page 297 Exercise 8.4
  2. //Saying program found x # of this character in string.
  3.  
  4.  
  5. public class Test
  6.  
  7. {
  8. //variables
  9.  
  10. char a = a;
  11.  
  12. {
  13. public static void main(String[] args)
  14. {
  15. if (args.length != 2)
  16. {
  17. System.out.println("Not enough arguments");
  18. }
  19. else
  20. {
  21. Count(args[0], args[1].charAt(0));
  22. }
  23. }
  24.  
  25. public static int Count(String str, char a)
  26. {
  27. int charCount = 0;
  28.  
  29. System.out.print(str);
  30. System.out.print(a);
  31.  
  32. for (int i = 0; i<str.length(); i++)
  33. {
  34. if (str.charAt(i) == a)
  35. {
  36. charCount ++;
  37. }
  38.  
  39. return charCount;
  40. }
  41. }
  42. }
  43. }
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 2,467
Reputation: masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of 
Solved Threads: 267
Moderator
masijade's Avatar
masijade masijade is offline Offline
Nearly a Posting Maven

Re: Making a program that counts the # of characters

 
0
  #2
Nov 8th, 2007
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
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,515
Reputation: Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future 
Solved Threads: 523
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: Making a program that counts the # of characters

 
0
  #3
Nov 8th, 2007
... and also that
  1. char a = a;
won't compile...
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 89
Reputation: leroi green is an unknown quantity at this point 
Solved Threads: 0
leroi green leroi green is offline Offline
Junior Poster in Training

Re: Making a program that counts the # of characters

 
0
  #4
Nov 8th, 2007
what should i do about that part that won't compile because i don't see why its going thru on the cmd but then not working
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,515
Reputation: Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future 
Solved Threads: 523
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: Making a program that counts the # of characters

 
0
  #5
Nov 8th, 2007
A char literal needs to be in single quotes
  1. char a = 'a';
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 89
Reputation: leroi green is an unknown quantity at this point 
Solved Threads: 0
leroi green leroi green is offline Offline
Junior Poster in Training

Re: Making a program that counts the # of characters

 
0
  #6
Nov 8th, 2007
okay okay, thanx
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 89
Reputation: leroi green is an unknown quantity at this point 
Solved Threads: 0
leroi green leroi green is offline Offline
Junior Poster in Training

Re: Making a program that counts the # of characters

 
0
  #7
Nov 8th, 2007
oh i also just noticed that i totally had the main method in the wrong place. trying to compile now.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 89
Reputation: leroi green is an unknown quantity at this point 
Solved Threads: 0
leroi green leroi green is offline Offline
Junior Poster in Training

Re: Making a program that counts the # of characters

 
0
  #8
Nov 8th, 2007
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?


  1. public class Test
  2.  
  3. {
  4. //variables
  5. char a = 'a';
  6.  
  7. public static void main(String[] args)
  8.  
  9. {
  10. if (args.length < 1)
  11. {
  12. System.out.println("Not enough arguments");
  13. }
  14. else
  15. {
  16. Count(args[0], args[1].charAt(0));
  17. }
  18. }
  19.  
  20. public static int Count(String str, char a)
  21. {
  22. int charCount = 0;
  23.  
  24. System.out.print("The number of character 'a' found is: ");
  25.  
  26.  
  27. for (int i = 0; i<str.length(); i++)
  28. {
  29. if (str.charAt(i) == a)
  30. {
  31. charCount ++;
  32. }
  33. System.out.print(charCount);
  34. }
  35. return charCount;
  36. }
  37. }
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 5
Reputation: sb7000 is an unknown quantity at this point 
Solved Threads: 1
sb7000 sb7000 is offline Offline
Newbie Poster

Re: Making a program that counts the # of characters

 
0
  #9
Nov 8th, 2007
Have you tried regex? Also an amazing program for Java is Eclipse, with it you wont need to run the program through cmd, although I would recommend that you run it through cmd before you submit your code. Look up Eclipse, it will save you many a frustrated hour.
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 6,143
Reputation: jwenting is just really nice jwenting is just really nice jwenting is just really nice jwenting is just really nice 
Solved Threads: 212
Team Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: Making a program that counts the # of characters

 
1
  #10
Nov 9th, 2007
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.
As people are clearly allowed to attack me but I'm not allowed to defend myself, I no longer post to this site.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 1875 | Replies: 15
Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC