This discussion is archived
1 2 Previous Next 20 Replies Latest reply: Feb 13, 2007 10:36 AM by 807599 RSS

character array

807599 Newbie
Currently Being Moderated
Hello,

I have an exercice.

I have a character array with a complete sentence inside. For instance:

I go to the swimming pool.

How to extract word by word and to print it out? For instance:

I
go
to
the
...

Thanks for any help.
  • 1. Re: character array
    807599 Newbie
    Currently Being Moderated
    What do you have so far? Are there any classes in the API that were suggested for use? Or are you expected to create a solution from scratch?
  • 2. Re: character array
    807599 Newbie
    Currently Being Moderated
    Are there any classes in the
    API that were suggested for use?
    Yes I have to use Class Character, Class String.

    But forgot to tell that this character array is fed with different sentences.
  • 3. Re: character array
    807599 Newbie
    Currently Being Moderated
    http://java.sun.com/j2se/1.5.0/docs/api/java/lang/String.html
    http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Character.html

    Those are the apis for the String and Character classes, read through the available methods and see what you can use to accomplish this.

    Note that this is a very important skill to learn if you want to become a good programmer. Being able to read the documentation and find the solution to any problem will make your life much easier.
  • 4. Re: character array
    807599 Newbie
    Currently Being Moderated
    Note that this is a very important skill to learn if
    you want to become a good programmer. Being able to
    read the documentation and find the solution to any
    problem will make your life much easier.
    Thanks for the counsel, but this is what I do for the past 2 days - no result.

    I would appreciate a little hint. The reason I don't post the code in here, is that I want to learn this Java.

    Thanks anyway for the addresses.
  • 5. Re: character array
    807599 Newbie
    Currently Being Moderated
    StringTokenizer stz=new StringTokeizer(new String(char[] value)," ");
    Look at StringTokenizer class to obtain tokens next

    You can award the dukes if you have found it useful.
  • 6. Re: character array
    807599 Newbie
    Currently Being Moderated
    StringTokenizer stz=new StringTokeizer(new
    String(char[] value)," ");
    Look at StringTokenizer class to obtain tokens next

    You can award the dukes if you have found it useful.
    This is precisely what I cannot use.
  • 7. Re: character array
    807599 Newbie
    Currently Being Moderated
    StringTokenizer stz=new StringTokenizer(new String(char[] value)," ");
    Look at StringTokenizer class to obtain tokens next

    You can award the dukes if you have found it useful.
    You can now use stz.nextToken( ) to obtain the tokens i.e to obtain each word from that array.
  • 8. Re: character array
    807599 Newbie
    Currently Being Moderated
    Then use
    int indexOf(String str, int fromIndex)  
    Which returns the index of the first occurance of str where in str you can specify a balank space.Once you obtain that you can obtain the string till that index and print it.Continue till you obtain all the words.

    The dukes
  • 9. Re: character array
    807599 Newbie
    Currently Being Moderated
    It seems strange that assignments like this tie themselves into knots getting you not to use classes that would make things easier (and result in much more readable code). That always annoyed me.

    I'm not entirely sure what the constraints of the assignment are - from what you've given, it looks like you could just loop through the string, examining one character at a time (see the APIs for that) and printing them out until you get to a space, in which case you'd start a new line.

    If storing each word separately is a requirement, the theory is much the same, but placing each word in some form of store, such as a list/array of strings.

    Also, I think QFN might be asking for some dukes. He's always subtle about that.
  • 10. Re: character array
    807599 Newbie
    Currently Being Moderated
    // char[] array
    String[] arrayOfWords = new String(array).split("\\s");
  • 11. Re: character array
    807599 Newbie
    Currently Being Moderated
    Many thanks for your help.

    I'll try your suggestions.

    Just a little questions?
    Some people ask for dukes! What's that? How can I give them to you?
  • 12. Re: character array
    807599 Newbie
    Currently Being Moderated
    Some people ask for dukes! What's that? How can I give them to you?
    We're just joking. The dukes only cheapen it for us. (Hehehe, questie...)
  • 13. Re: character array
    807599 Newbie
    Currently Being Moderated
    If you have a char array why not just create a new String? One constructor takes a char array as a parameter.

    Ooops! Didn't read reply #10.
  • 14. Re: character array
    807599 Newbie
    Currently Being Moderated
    // char[] array
    String[] arrayOfWords = new
    String(array).split("\\s");
    Hello,

    I wanted to look at this method. It doesn't exist in API version 1.3.1!
1 2 Previous Next