The Artima Developer Community
Sponsored Link

Java Answers Forum
Initial Cap Words Within Text File

7 replies on 1 page. Most recent reply: Oct 17, 2005 12:57 AM by Kondwani Mkandawire

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 7 replies on 1 page
paul

Posts: 7
Nickname: larkinp317
Registered: Mar, 2005

Initial Cap Words Within Text File Posted: Oct 13, 2005 5:33 AM
Reply to this message Reply
Advertisement
Hi,

I need to Initial Cap each word from a text file. I can read the required file (see below) but then want to Initial Cap each word and save to another file

Any help please

import java.io.*;

public class ReadFile {


/**
* @param args the command line arguments
*/
public static void main(String[] args) {

String output = "";




FileReader in = new FileReader("e:/assfile.txt");

int inChar = in.read();

while(inChar != -1)
{
output += (char)inChar;

inChar = in.read();


}
System.out.println(output);


}
}


Kondwani Mkandawire

Posts: 530
Nickname: spike
Registered: Aug, 2004

Re: Initial Cap Words Within Text File Posted: Oct 13, 2005 6:26 AM
Reply to this message Reply
Its an extremely easy problem one which I'll respond when
you go to Wall Mart to purchase the courtesy to indent
your code.

When you were making that post there were simple instructions to the right of the Text Area in which you typed your code.

http://www.artima.com/forums/howtopost.html

If you can't afford to purchase some courtesy at Wal Mart
the at run to Chapters and purchase a Book on Java I/O
you might find its cheaper.

Kondwani

paul

Posts: 7
Nickname: larkinp317
Registered: Mar, 2005

Re: Initial Cap Words Within Text File Posted: Oct 13, 2005 6:39 AM
Reply to this message Reply
Sorry for the indiscretion, will try better next time.

paul

Posts: 7
Nickname: larkinp317
Registered: Mar, 2005

Re: Initial Cap Words Within Text File Posted: Oct 13, 2005 6:48 AM
Reply to this message Reply
Maybe this is better

Still need the same answer: How do I Initial Cap each word that is within the text file

import java.io.*;
 
public class ReadFile {
 
 
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
 
String output = "";
 
 
 
 
FileReader in = new FileReader("e:/assfile.txt");
 
int inChar = in.read();
 
while(inChar != -1)
{
output += (char)inChar;
 
inChar = in.read();
 
 
}
System.out.println(output);
 
 
}
} 
 


Please any helpers now

Kondwani Mkandawire

Posts: 530
Nickname: spike
Registered: Aug, 2004

Re: Initial Cap Words Within Text File Posted: Oct 13, 2005 7:11 AM
Reply to this message Reply
No its not better!!! Its not legible, it just has extra pretty little
colors... Have you never heard of indentition???

Here is what Java Code looks like in the professional and academic environment:

public class ICantWriteProperCode{
    private String var1;
    private String var2;
    
    public ICantWriteProperCode(){
        System.out.println("Why Not?");
    }
    
    public void indentMethodsAfterBrackets(int val){
       
    }
}


But just to be nice when you read from the file read via a line, tokenize that
line Separated by a " "; and for each of those StringTokenizers do
//  Use a BufferedReader to read in Lines of your file
//  You may want to stick this in a while loop
//  something to the effect of while(buf.readLine() ! = null)
//  I'm too lazy to formulate the actual loop but within the
//  loop you have this:
StringTokenizer tokenizer = new StringTokenizer(buf.readLine(), " ");
while(tokenizer.hasMoreTokens()){
    String token = tokenizer.next();
    String upper = ""+token.charAt(0);
    String result = null;
    if(token.length() > 1)
        result = upper+token.substring(1);
   else
       result = upper;
}
//  at the end you join result with output.

Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: Initial Cap Words Within Text File Posted: Oct 16, 2005 1:25 PM
Reply to this message Reply
First, I think this problem should be solved with a regular expression.

Second, Kondwani, I still don't think that code looks very professional; the curlies are in the wrong place. ;)

paul

Posts: 7
Nickname: larkinp317
Registered: Mar, 2005

Re: Initial Cap Words Within Text File Posted: Oct 17, 2005 12:23 AM
Reply to this message Reply
Hi, all

I am still struggling with this problem. Have tried Tokenizing and some array stuff, but still no joy.

The first part of the code works (see 1 above) my text is output to screen, I have then done some simple text counting. My next task is to look at the output text and change all word, 1st letters, to upper case and output this to screen followed by a save to new file as backup.

You may guess but I am new to java and my task is to demonstrate simple java file handling techniques.

Any helpers please

Kondwani Mkandawire

Posts: 530
Nickname: spike
Registered: Aug, 2004

Re: Initial Cap Words Within Text File Posted: Oct 17, 2005 12:57 AM
Reply to this message Reply
> First, I think this problem should be solved with a
> regular expression.

That would be the easy way to solve the problem if the OP understood
the concept of regular expressions. Gauging from the fact that he/she
is too lazy to follow instructions, I doubt that he would care to
google the concept. Even if he did, the thought of creating Regulations
via expressions/grammars and linking that concept to the readLine
concept hence parsing the whole line and possibly the next if the
sentence spills over, would seem like "Rocket Science" to this person.

Flat View: This topic has 7 replies on 1 page
Topic: Problem:compliles but won't run Previous Topic   Next Topic Topic: Save With a given Path

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use