I'm trying to make a program where a user can post a comment and it'll be able to extract the words, e.g.
I love to #program in #java
would show the output
#program
#java
What I have currently is not running, although there is no errors detected.
class userInput {
public static Scanner input = new Scanner(System.in);
public static String readString(String message){
System.out.println(message);
String readValue = input.nextLine();
return readValue;
}
public static int readInt(String message){
System.out.println(message);
int readValue = input.nextInt();
input.nextLine();
return readValue;
}
public static double readDouble(String message){
System.out.println(message);
double readValue = input.nextDouble();
input.nextLine();
return readValue;
}
public static void close(){
input.close();
}
public static void main(String[] args) {
String post [] = new String [5];
String userPost = "";
userPost = userInput.readString("Type your post");
post[0] = userPost;
String hashtags ="";
for (int i = 0; i<post.length && post[i]!=null;i++){
String[]words = post[i].split(" ");
for(int j=0;j<words.length;j++){
if(words[j].trim().startsWith("#")){
hashtags+=words[j].trim() + " ";
}
}
}
if(hashtags.trim().isEmpty())
System.out.println("No hashtags were typed");
else
System.out.println("Hashtags found:" + hashtags);
}
}