Java newb, needs some pointers.

Associate
Joined
18 Aug 2010
Posts
347
Location
London
I'm not asking for anybody to todo the work for me and nor do I want anyone too as that would kinda flaw the point of uni but I really need some help.


If you could just send me a message I would greatly appreciate it.

Thanks for your time.
 
Last edited:
There are no pointers in Java.

Sorry, couldn't resist :p Best of luck with it though! :)

I'd suggest just posting your questions in this thread rather than trying to get 1-2-1 help with it though. People are more likely to help if they can see your questions without pseudo-committing to helping by contacting you.
 
You could try using the .Split function, which would take each string you input and separate it based on your defined criteria which in this case may be a space and then count how many words you've got. The only issue with this is, if there is two spaces in a line of text next to each other that or a space character at the end of a sentence which granted shouldn't be there.
 
Best way would be to load a line into a string array and cycle through each character comparing it to a space character and count it that way.

Not optimized but simple way to do it.
 
Code:
System.out.printf("%d spaces in : \" %s \" ", place, countCh( place, ' ' ) );

Shouldn't that be

Code:
System.out.printf("%d spaces in : \" %s \" ", countCh( place, ' ' ), place );

Instead?
 
Edit: Too slow. :o

Seems like you've got your inputs the wrong way round here:

PHP:
System.out.printf("%d spaces in : \" %s \" ", place, countCh( place, ' ' ) );

Your trying to format "place" (a String) as a decimal integer (%d)

I think you want the count before "place" in your string right? So just swap them round in the arguments for your printf and it should work fine.

Adding square braces should be fine for that. It works ok for me at least. :confused:


I've added the results of the changes I made to make it work, you can ignore the changes I've made to get the input if your way already works for you.

PHP:
public class main
{
    public static void main(String args[]) {
        try {
            System.out.print("#Enter text : ");

            InputStreamReader converter = new InputStreamReader(System.in);
            BufferedReader in = new BufferedReader(converter);
            String place = in.readLine();


//            String place = BIO.getString();

            while (!place.equals("END")) {
                System.out.printf("[%d] spaces in : \"%s\" ",
                        countCh(place, ' '), place);
                System.out.println();
                System.out.print("#Enter text : ");
//                place = BIO.getString();
                place = in.readLine();
            }
        } catch (IOException e) {

        }
    }

    public static int countCh(String s, char c)
    {
        String sLC = s.toLowerCase();

        int count = 0;
        for (int i = 0; i < sLC.length(); i++)
        {
            if (sLC.charAt(i) == c)
                count++;
        }
        return count;
    }
}
#Enter text : Hello Xerco!
[1] spaces in : "Hello Xerco!"
#Enter text :
 
You've got 'c' defined as one of your parameters for countCh, as well as in the method itself. :)

That parameter isn't being used in the method any more, so you'll probably just want to remove it and place the space character as part of your 'if' statement. You'll also need to shuffle your return outside of the for statement if you want it to work.

I'm not sure how in depth this piece of work is, but if you wanted to be able to pass in characters (like you were previously) you could pass them in as a list or array. :)

PHP:
    public static int countCh(String s)
    {
        String sLC = s.toLowerCase();

        int count = 0;
        for (int i = 0; i < sLC.length(); i++)
        {
            char c = sLC.charAt(i);
            if (c == ' ' || c=='a' || c=='e' || c=='i' || c=='o' || c=='u') {
                count++;
            }
        }
        return count;
    }

Are you using a syntax highlighting editor or IDE? Or doing this in notepad? You might find it a bit easier with one if not. :)
 
Last edited:
Sorry i dont fully understand what your saying, I need to do the same thing as counting the spaces but this time counting all vowels in an input. But i dont understand how it is not as simple as just adding

char c = sLC.charAt(i);
if (c == ' ' || c=='a' || c=='e' || c=='i' || c=='o' || c=='u') {
count++;

to what I already had.

Yes im using an IDE BlueJ

Thank you for this help by the way, been great.


Edit:

Fixed it :D
 
Last edited:
I think text isn't being converted to lowercase, otherwise it'd print abcde instead of aBcDe.

try text = text.toLowerCase(); maybe?

Or have a new variable like textLC = text.toLowerCase(); so you can print the right input string when it's done.
 
Well I suppose if you're not using an IDE and probably using notepad then things like that would be easy to miss.

May I ask what course this work is for? I thought at uni it'd be much harder :)
 
Back
Top Bottom