I am creating a piece of code to take a hashmap called wordFrequencies, which contains some words and how many times they appear in a given string.
To spare details, the words need to line up so I am trying to create code to add spaces to the start of the words until they are all in alignment. I am doing this by comparing their lengths.
The issue I am having is with the while loop, as word is defined only in the for loop and I am not sure how to define it to be used within the while loop, as there is know such thing as a "while each" loop.
// loop to determine length of longest word
int maxLength = 0;
for (String word : wordFrequencies.keySet()){
if (word.length() > maxLength) {
maxLength = word.length();
}
}
// loop to line up the words in the graph / table
while (word.length() < maxLength){
word = " " + word; // if word is shorter than longest word, add spaces to the start until they line up
}