Java: While-Loop question

I am understanding what is going on with this bit of code here, but I wanted to see if I could print the longest string value to the terminal and thus ending the loop as well. I have tried several different ways but to no avail. Any help would be appreciated.

To put what I am getting at into an example:
Say I enter the phrase “Take me home tonight”.

longest, at the end of the loop, will equal 7 and I would like to end the loop and print that value.

count = 0;
longest =0;
String myString = new String();
while (input.hasNext()){
myString = input.next();
if (myString.length() == longest)
count++;
else if (myString.length() > longest){
longest = myString.length();
count = 1;
}
}