If you are learning Java, I would suggest focusing first on modeling problems with Java classes and methods.
The Markov chain is a model or statistical development of the seminal text, right? Using it to model text, he usually describes how often each word is followed by each other word. (usually you would split the text at word boundaries). It looks like he needs a class; it could be called MarkovChain .
In the MarkovChain class, you need something to hold on to every word that appears in the text and maps that word to other words in the text, as well as the number of other words.
Suppose the word "and". In the text “and” follows “four”, and “then” - three times. So you will need some data structure to hold something like this:
and --> the (4) then (3)
One way to do this is to use an ArrayList to store all the words, and then Map<T1,T2> , which contains the relationship between the words and the frequency of the next words. In this case, T1 is probably a string, and T2 is probably an ArrayList pair — a string and a (integer) counter for that string.
But wait, now you don’t need the ArrayList<> base for storing words, because they are just keys on the map.
... and so on. The next step would be to figure out how to populate this data structure. This is probably an internal (private) method that is called when the caller creates the MarkovChain class with the source text.
You probably also want the MarkovChain class to detect another open method that calls when they want to generate some random sequence from the chain, relying on probabilities based on frequency.
...
This is just one way to think about modeling a problem.
In any case, I would like to focus on this modeling / design exercise before writing code.