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;
    }
   }
  }
}

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.

... and also that

char a = a;

won't compile...

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

A char literal needs to be in single quotes

char a = 'a';

okay okay, thanx

oh i also just noticed that i totally had the main method in the wrong place. trying to compile now.

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;
	}
}

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.

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.

// Your whole program code is reassembled and bugged as below. kido!!
public class Test

{
    //variables
    char a = 'a';

    public static void main(String args[])

    {
        if (args.length < 1)
        {
            System.out.println("Not enough arguments");
        }
        else
        {
                                                System.out.print("The number of character 'a' found is: ");


            Syste.out.print(Count(args[1]));
        }
    }

    public  int Count(String str)
    {
        int charCount = 0;

        for (int i = 0; i<str.length(); i++)
        {
            if (str.charAt(i) == 'a')
            {
                charCount ++;
            }
        }
        return charCount;           

    }
}
Member Avatar for iamthwee

>is reassembled and bugged as below
Shouldn't you try and debug it then?

I recommend DDT.

Your Program expecting to have been passed 2 argument via cmd line.
1. String which is to be searched
2. the char to search.

You are directly passing 2nd argument's first char in call of Count(). So, the class attribute (variable for you) a is of no use in your code.
If a should contain the character you want to search, then do not hardcode it with 'a' but initialise it as static & assign the a with args[1].charAt(0).
Once you have done this, u should not pass it through Count() as it is directly accessible to Count Method.

& Finally print charCount after for loop, & if you are not using return value, make the void Count & do not return value.

Dear iamthwee & jwenting,
Both of you have really understood me. I indirectly want leroi green to debug the reassembled code and again reassemble it by taking out the variable declaration as well as putting the main method last. of course an 'm' is missing somewhere.

Thanq
Happy programming.

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.