• 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Words with capital letters

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
author and iconoclast
Posts: 24203
44
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...

 
Ranch Hand
Posts: 194
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 24203
44
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 194
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Never thought about that, and to think they teach us that in college. Thanks.
 
Marshal
Posts: 74617
335
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 2458
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is the Character#isUpperCase().
 
Bartender
Posts: 1111
Eclipse IDE Oracle VI Editor
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic