Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Isolating a single word from user input...

  1. #1
    Junior Member
    Join Date
    Jan 2012
    Posts
    7
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Isolating a single word from user input...

    I need to isolate a word that starts with a hashtag (#) from a string of user input. For example if the user enters "Hi how #are you today" i need the program to print out "#are".

    My thought process was to try to isolate the substring from the index of the # to the next space, but I do not know how to get it to recognize the index of the next space instead of just the first space in the string. So my code for this action looks a little like this right now:

    tweet = keyboard.nextLine();
    int pos = tweet.indexOf("#");
    String hashtag = tweet.substring(pos, " ");
    System.out.println("Hashtag: " + hashtag);

    ANY advice would be helpful. Also I have not learned about loops or arrays yet so if there is anyway to do this without them that would be great!


  2. #2
    Member snowguy13's Avatar
    Join Date
    Nov 2011
    Location
    In Hyrule enjoying a chat with Demise and Ganondorf
    Posts
    339
    My Mood
    Happy
    Thanks
    31
    Thanked 48 Times in 42 Posts

    Default Re: Isolating a single word from user input...

    I think your problem can be easily solved. There are many ways in which you can use the <String>.indexOf() method...
    Take a look at this one.
    Use highlight tags to help others help you!

    [highlight=Java]Your prettily formatted code goes here[/highlight]

    Using these tags makes your code formatted, and helps everyone answer your questions more easily!




    Wanna hear something funny?

    Me too.

  3. The Following User Says Thank You to snowguy13 For This Useful Post:

    ninjaBob (January 15th, 2012)

  4. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,571
    Thanks
    49
    Thanked 2,232 Times in 2,204 Posts

    Default Re: Isolating a single word from user input...

    get it to recognize the index of the next space
    Read the API doc for the indexOf() method. It's all there.

  5. The Following User Says Thank You to Norm For This Useful Post:

    ninjaBob (January 15th, 2012)

  6. #4
    Junior Member
    Join Date
    Jan 2012
    Posts
    7
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Isolating a single word from user input...

    THANK YOU GUYS! i hadn't thought of trying it that way! Needless to say, it works now!

Similar Threads

  1. Valid user input
    By ChristopherLowe in forum Java Programming Tutorials
    Replies: 1
    Last Post: June 21st, 2011, 04:53 PM
  2. Valid user input
    By ChristopherLowe in forum Java Code Snippets and Tutorials
    Replies: 1
    Last Post: June 21st, 2011, 04:53 PM
  3. User Input File Name
    By PineAppleKing in forum Java Theory & Questions
    Replies: 12
    Last Post: June 3rd, 2011, 10:23 AM
  4. Printing input word within ascii triangle
    By tla280 in forum Java Theory & Questions
    Replies: 1
    Last Post: May 1st, 2011, 08:53 PM
  5. User Input Loop
    By cfmonster in forum Loops & Control Statements
    Replies: 7
    Last Post: August 24th, 2009, 01:52 PM

Tags for this Thread