• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Scanner help

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, I’m trying to find capitalized names in a sentence. For example: “His name is Bugs Bunny and he loves carrots. “ The code below will get Bugs Bunny but then it skips “and” because of the scan.next(); Is there any way to NOT skip the “and”? I want the scanner to continue scanning the same sentence in case of another name and I don’t want to miss any of the tokens.

The scanner goes through the sentence with a while loop, however when it finds a keyword (after the keyword might be a name) from the database in the sentence. It will check if the following word is capitalized and scans until it doesnt find a capitalized word anymore.


 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

S. Jones wrote:Hi, I’m trying to find capitalized names . . . The code below will get Bugs Bunny . . .

Will it? I would hope it wouldn’t. It should find “Bugs” and “Bunny” separately. You need to go through the execution order of the application with a pencil and paper. Note how many times the loops and blocks are repeated. Also write down whether the isKword variable is changed in each iteration. I suspect you are turning it false before you have exhausted the supply of capitalised words, but I am not sure and have not gone through your code at all carefully.

And welcome to the Ranch
 
S. Jones
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

S. Jones wrote:Hi, I’m trying to find capitalized names . . . The code below will get Bugs Bunny . . .

Will it? I would hope it wouldn’t. It should find “Bugs” and “Bunny” separately. You need to go through the execution order of the application with a pencil and paper. Note how many times the loops and blocks are repeated. Also write down whether the isKword variable is changed in each iteration. I suspect you are turning it false before you have exhausted the supply of capitalised words, but I am not sure and have not gone through your code at all carefully.

And welcome to the Ranch



Thank you for replying! I'm sorry, a part of the code was repeated somehow. I edited it to the correct one.
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 style
  • Give 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.
     
    Ranch Hand
    Posts: 44
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    One more thing.. try to learn in which cases you can use while loop or do-while loop... you should not only use if-else... Happy learning
     
    reply
      Bookmark Topic Watch Topic
    • New Topic