Please don’t go back and make big changes to posts after they have been replied to, otherwise the replies look like nonsense.
I have not tried to follow the loops, but writing
while (Character.isUpperCase(nextWord.charAt(0)) && scan.hasNext())... looks to me as if you are guessing. If you keep guessing long enough, you might hit on a solution which works, but you won’t know why and you won’t know which option works, and you won’t be able to solve your next problem.
I think you are making things difficult with the database connection. Isolate that by creating a Set<
String> to hold your keywords. Rather than the loop to determine whether your word is a keyword, you can simply use
if (myKeywordsSet.contains(word)) ... or similar. Fill the set from the database when you initialise it. The
contains() method has very fast performance. Print out the set, so you can see which keywords it contains.
Don’t declare more than one variable or field per line, if you want the code to be legible. Spaces after commas and around binary operators, including =, please. If you use the + operator on Strings, do all your catenating in one statement. Otherwise use a StringBuilder, for reasons of better performance.
You won’t get “Bugs Bunny”, but “ Bugs Bunny”. Note the subtle difference. You can avoid the odd spaces getting in there in the first place, so you don’t need the
trim() call.
How do you tell where a name ends? What will your program do with input like
Scanner scan=new Scanner("His name is Bugs Bunny and he loves carrots and my name is Campbell Ritchie."); What will happen to the full stop at the end? Will it pick up Ritchie which will be the last token?
I think
you should...
Tidy up the code styleGive a better name to the isKword variable. Maybe nextIsName.You can reset nextIsName something like this:- nextIsName = nextIsName && Character.isUpperCase(...) || myKeywordSet.contains(nextWord); I shall let you work out how that works for yourself.Use one loop with while (scan.hasNext()) ...Consider a different delimiter for the scanner.Use a StringBuilder for catenating names. You can empty a string builder with .setLength(0), and get its contents as a String simply with toString()But before you do, go through your loop with a pencil and some paper. Then you will see what you really want to do. When you have finished, the loop will probably be about half the length the current loop is.