Finding hashtags in a input string from a user

Viewed 25

I am running a program that is supposed to be able to find every word starting with a hashtag and outputting it but I get thrown this error :

Exception in thread "main" java.util.NoSuchElementException: No line found
    at java.base/java.util.Scanner.nextLine(Scanner.java:1651)
    at userInput.readString(Menu.java:9)
    at Menu.main(Menu.java:198)

I am relatively new to Java so I am unsure how to fix this. Some input would be greately appreciated

My code:

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();
        }
    }
    
     
    
     String post [] = new String [100];
                        String userPost = "";
                        userPost = userInput.readString("\nType 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 );   
0 Answers
Related