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! :)
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.
Re: Isolating a single word from user input...
Quote:
get it to recognize the index of the next space
Read the API doc for the indexOf() method. It's all there.
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!