Results 1 to 12 of 12
  1. #1
    kmjt is offline Member
    Join Date
    Jan 2011
    Posts
    3
    Rep Power
    0

    Default Kind of stuck on starting a problem..

    Here is the question:

    Write a program that will read a line of text as input and then display the line with the first word moved to the end of the line. For example, a possible
    sample interaction with the user might be
    Enter a line of text. No punctuation please. Java is the language I have rephrased that line to read: Is the language Java
    Assume that there is no space before the first word and that the end of the first word is indicated by a blank, not by a comma or other punctuation. Note that the new first word must begin with a capital letter.
    From looking at the question i'm guessing that I need to somehow get my program to recognize what the first word in the inputted sentence is. How exactly would I do this? I'm guessing it has something to do with stopping when the program reads an empty space like " " but i'm not sure how to do this.

    Perhaps finding the length of the first word and than printing all of the characters of the first word after the entered sentence? But than I don't know how to get rid of the first word from the start of the sentence :confused:

    All I really need is to know how to split and recognize words in a sentence. I know there was a split feature in python that recognizes words because of blank spaces in between them: " "

  2. #2
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    The String class also has a split method. When faced with problems like these, it's always a good idea to check out the API, there's a good possibility that what you're trying to do is already implemented. Just google java classname, usually the first hit is the API.
    Ever seen a dog chase its tail? Now that's an infinite loop.

  3. #3
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Quote Originally Posted by kmjt View Post
    All I really need is to know how to split and recognize words in a sentence. I know there was a split feature in python that recognizes words because of blank spaces in between them: " "
    Uhm, see the API docs for String and its split(regex) method, maybe?

    Edit: Too slow!

  4. #4
    JavaHater is offline Senior Member
    Join Date
    Dec 2010
    Posts
    165
    Rep Power
    4

    Default

    Quote Originally Posted by kmjt View Post
    All I really need is to know how to split and recognize words in a sentence. I know there was a split feature in python that recognizes words because of blank spaces in between them: " "
    the String class has a split method
    Java Code:
    String s[] =  myString.split("\\s+");
    System.out.println( "This is your first word: " + s[0] )
    Or you can go the longer way. Use indexOf() to get the index of the first space. After that do a substring() .

  5. #5
    Debugger is offline Member
    Join Date
    Jan 2011
    Posts
    27
    Rep Power
    0

    Default

    check out string tokenizer, this will let you break the sentence up into individual words, then you could put each word in an array and re-arrange that. Just a thought

  6. #6
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Please No! StringTokenizer has a load of problems (not the smallest, although most well known, of them is it's inability to properly handle non-ASCII character sets) and its use has been officially discouraged (and the class all but deprecated) for a decade or more now.

  7. #7
    Debugger is offline Member
    Join Date
    Jan 2011
    Posts
    27
    Rep Power
    0

    Default apprecieted

    Your response is exactly why I joined this forum. To gain more knowledge other than what is thought by my professors. Because I have been to both a community college and am in a very well known technical school now and I have still been taught to use the tokenizer. So thanks I will start looking into better options.

  8. #8
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    The easiest way to do this would be to use split(" ") on the input string to store it in an array, then loop through the array printing the items, obviously you have to figure out how to print the words correctly, but this should be more than enough to start.

    While slightly more challenging, but still a fine way is to use str.substring() or charAt() to loop through until you find the first space, then from 0 to when the first space is found is the first first number, which you can extract with the substring method.

  9. #9
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    My 0.02 cents

    Since you will need to use charAt and substring for the first letter anyway, I would use charAt and substring for the whole thing and not use split. Then you do not need a loop and if you are masochistic you can reduce it to a single line of code (after getting the input).

  10. #10
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Quote Originally Posted by Junky View Post
    My 0.02 cents

    Since you will need to use charAt and substring for the first letter anyway,
    Huh? What does the first "letter" have to do with anything?
    I would use charAt and substring for the whole thing and not use split. Then you do not need a loop and if you are masochistic you can reduce it to a single line of code (after getting the input).
    You're right in one thing though. since it will simply be moving the first word to the end, charAt to get the location of the first space, two substrings, and the addition of a space (and possibly the handling of any punctuation at the end of the sentence if necessary) would be better.

  11. #11
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    Quote Originally Posted by masijade View Post
    Huh? What does the first "letter" have to do with anything?
    The first letter of the second word (which becomes the first word) needs to be converted to uppercase and to do that the charAt and substring methods are needed.

  12. #12
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Missed that part, I guess, but then the first letter of the last word (I would assume) would also have to be lowercased.

Similar Threads

  1. Kind of stuck
    By Nicky Swans in forum New To Java
    Replies: 8
    Last Post: 10-22-2010, 03:46 PM
  2. Netbeans starting problem.
    By vsanandan in forum NetBeans
    Replies: 1
    Last Post: 06-18-2010, 01:23 PM
  3. Eclipse Europa Starting problem
    By prashant.kadake in forum Eclipse
    Replies: 0
    Last Post: 04-18-2009, 11:06 AM
  4. starting problem
    By anithananduri in forum New To Java
    Replies: 1
    Last Post: 03-17-2009, 02:27 PM
  5. Problem in Starting JBOSS Server
    By 82rathi.angara in forum Networking
    Replies: 0
    Last Post: 06-16-2008, 03:58 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •