943,960 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Marked Solved
  • Views: 2289
  • Java RSS
You are currently viewing page 1 of this multi-page discussion thread
Nov 8th, 2007
0

Making a program that counts the # of characters

Expand Post »
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)
  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. }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
leroi green is offline Offline
93 posts
since Oct 2007
Nov 8th, 2007
0

Re: Making a program that counts the # of characters

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.
Moderator
Reputation Points: 1471
Solved Threads: 490
Industrious Poster
masijade is offline Offline
4,043 posts
since Feb 2006
Nov 8th, 2007
0

Re: Making a program that counts the # of characters

... and also that
Java Syntax (Toggle Plain Text)
  1. char a = a;
won't compile...
Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 839
Posting Genius
Ezzaral is offline Offline
6,761 posts
since May 2007
Nov 8th, 2007
0

Re: Making a program that counts the # of characters

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
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
leroi green is offline Offline
93 posts
since Oct 2007
Nov 8th, 2007
0

Re: Making a program that counts the # of characters

A char literal needs to be in single quotes
Java Syntax (Toggle Plain Text)
  1. char a = 'a';
Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 839
Posting Genius
Ezzaral is offline Offline
6,761 posts
since May 2007
Nov 8th, 2007
0

Re: Making a program that counts the # of characters

okay okay, thanx
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
leroi green is offline Offline
93 posts
since Oct 2007
Nov 8th, 2007
0

Re: Making a program that counts the # of characters

oh i also just noticed that i totally had the main method in the wrong place. trying to compile now.
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
leroi green is offline Offline
93 posts
since Oct 2007
Nov 8th, 2007
0

Re: Making a program that counts the # of characters

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?


Java Syntax (Toggle Plain Text)
  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. }
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
leroi green is offline Offline
93 posts
since Oct 2007
Nov 8th, 2007
0

Re: Making a program that counts the # of characters

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.
Reputation Points: 10
Solved Threads: 1
Newbie Poster
sb7000 is offline Offline
5 posts
since Nov 2007
Nov 9th, 2007
1

Re: Making a program that counts the # of characters

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.
Team Colleague
Reputation Points: 1658
Solved Threads: 331
duckman
jwenting is offline Offline
7,719 posts
since Nov 2004

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: I can't understand what's wrong
Next Thread in Java Forum Timeline: help with java





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC