Making a program that counts the # of characters
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.
//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;
}
}
}
}
leroi green
Junior Poster in Training
93 posts since Oct 2007
Reputation Points: 10
Solved Threads: 0
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.
masijade
Industrious Poster
4,253 posts since Feb 2006
Reputation Points: 1,471
Solved Threads: 494
... and also that
char a = a;
won't compile...
Ezzaral
Posting Genius
15,985 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847
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
leroi green
Junior Poster in Training
93 posts since Oct 2007
Reputation Points: 10
Solved Threads: 0
A char literal needs to be in single quotes
char a = 'a';
Ezzaral
Posting Genius
15,985 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847
leroi green
Junior Poster in Training
93 posts since Oct 2007
Reputation Points: 10
Solved Threads: 0
oh i also just noticed that i totally had the main method in the wrong place. trying to compile now.
leroi green
Junior Poster in Training
93 posts since Oct 2007
Reputation Points: 10
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?
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;
}
}
leroi green
Junior Poster in Training
93 posts since Oct 2007
Reputation Points: 10
Solved Threads: 0
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.
jwenting
duckman
8,392 posts since Nov 2004
Reputation Points: 1,662
Solved Threads: 337
>is reassembled and bugged as below
Shouldn't you try and debug it then?
iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
jwenting
duckman
8,392 posts since Nov 2004
Reputation Points: 1,662
Solved Threads: 337
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.
i feel exactly what you're saying and thanx for the help but it's so hard when you feel like you're drowning in this class if i don't have some type of edge but i just found i have a B+ in the course now which has kinda given me a boost of confidence in trying to keep doing it the 'hard' way i guess even though the IDE's seem like such a help.
thanx
leroi green
Junior Poster in Training
93 posts since Oct 2007
Reputation Points: 10
Solved Threads: 0