Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Words with capital letters

 
Arthur Donting
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey guys
I need to write a program that reads file and prints words that begin with capital letters on the screen.

For example input file is Test.txt which includes this text
Billy Bob is going to visit his aunt Muriel in Washington.

Than the output should be
Billy
Bob
Muriel
Washington


Could someone pleas help me?
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Welcome to JavaRanch!

Absolutely, we're here to help. We don't like to hand out code, though -- we like to help you figure it out for yourself. So my first question to you is, what do you have so far? If it's nothing, then what are your thoughts about what to do first?
 
Arthur Donting
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well... Here's what I have so far:


I had few ideas about what to do next.

First one is just to read line, break it into words, and put each word in array. Than just check each word weather its first letter belongs to a set of Capital letters. I had text how to compare sets somewhere so I would still need to separate words and create dynamic array or something like that (because number of words in line is unknown)

Another idea is to create cycle that scans one letter and if it's capital than prints it and keeps on scanning and printing symbol by symbol until it reaches space. After that again, scans one letter and checks weather it's capital or not and so on... I prefer this solution and have an idea how to write a cycle but have no idea how to scan one character in java if it's possible at all...

 
Ryan Beckett
Ranch Hand
Posts: 192
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Get the ascii values of the characters and check them against the ascii table.

 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ryan Beckett wrote:Get the ascii values of the characters and check them against the ascii table.


No, please don't do this! First of all, Java doesn't use ASCII, but Unicode; your "capital letters" could be in many different alphabets around the world, not just ASCII. Secondly, the Java libraries provide methods to do these kinds of tests correctly for Unicode. Look at the static method isUpperCase in the java.lang.Character class.
 
Arthur Donting
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Guys, I might have something here...



It works for me perfectly.
what do you think?
 
Ryan Beckett
Ranch Hand
Posts: 192
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Never thought about that, and to think they teach us that in college. Thanks.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49442
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You may find a Scanner easier to use than BufferedReader, assuming the file is a text file.
You may find the split method of the java.lang.String class useful, too.
 
Moojid Hamid
Ranch Hand
Posts: 120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Arthur do you know what "regular expressions" are? If you don't have a look at:

http://java.sun.com/docs/books/tutorial/essential/regex/

You can greatly simplify your code by using regular expressions.
 
Bauke Scholtz
Ranch Hand
Posts: 2458
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is the Character#isUpperCase().
 
Wendy Gibbons
Bartender
Posts: 1111
Eclipse IDE Oracle VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
have you looked at the String.split methods. They will take your input buffer and split it into words and pop the results into an array for you.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic