aspose file tools*
The moose likes Beginning Java and the fly likes Words with capital letters Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Words with capital letters" Watch "Words with capital letters" New topic
Author

Words with capital letters

Arthur Donting
Greenhorn

Joined: Mar 22, 2009
Posts: 10
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

Joined: Jul 08, 2003
Posts: 24187
    
  34

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?


[Jess in Action][AskingGoodQuestions]
Arthur Donting
Greenhorn

Joined: Mar 22, 2009
Posts: 10
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

Joined: Feb 22, 2009
Posts: 192
Get the ascii values of the characters and check them against the ascii table.

Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

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

Joined: Mar 22, 2009
Posts: 10
Guys, I might have something here...



It works for me perfectly.
what do you think?
Ryan Beckett
Ranch Hand

Joined: Feb 22, 2009
Posts: 192
Never thought about that, and to think they teach us that in college. Thanks.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40061
    
  28
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

Joined: Mar 07, 2009
Posts: 120
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

Joined: Oct 08, 2006
Posts: 2458
There is the Character#isUpperCase().
Wendy Gibbons
Bartender

Joined: Oct 21, 2008
Posts: 1107

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Words with capital letters