Replacing letters through StreamTokenizer

Hi,
I am writing a program which reads from a text file, passes it into a StreamTokenizer, looks for a particular word and capitalises the first letter (in this case "apples" to "Apples"). It's all fine, except when the word I'm looking for appears in the middle of a sentence, in which case, it seems to ignore it completely. I'm probably doing something really stupid, but I'd appreciate it if anyone could help me!
Input file:
John likes apples. apples are healthy.
The apples John eats are red, sometimes green. Most
apples come from farms.
Current output file:
John likes apples. Apples are healthy.
The apples John eats are red, sometimes green. Most
apples come from farms.
What output file is supposed to be:
John likes Apples. Apples are healthy.
The Apples John eats are red, sometimes green. Most
Apples come from farms.
Code:
//Filename:FileReader.java
import java.io.*;
public class FileReader
     public static void main(final String[]Args) throws IOException, FileNotFoundException
          //declares and instantiates the array aFileNames
          String[] aFileNames = new String[3];
          //read in the input & output file names and places them into an array
          for(int tArgNames = 0; tArgNames < Args.length; tArgNames ++)
               aFileNames[tArgNames] = Args[tArgNames];
          //instantiates a buffered reader
          BufferedReader tTextIn = new BufferedReader(new FileReader(aFileNames[0]));
          //declares and instantiates a stream tokenizer
          StreamTokenizer tStreamTokens = new StreamTokenizer(tTextIn);
          //declares a print writer for output file
          PrintWriter tTextOut = null;
          //instantiates the print writer for output file
          tTextOut = new PrintWriter(new FileWriter(aFileNames[1]));
          //declares a print writer for log file
          PrintWriter tTextLog = null;
          //instantiates the print writer for log file
          tTextLog = new PrintWriter(new FileWriter(aFileNames[2]));
          //Writes name of files being opened to log file
          tTextLog.println("File being opened: " + aFileNames[0]);
          tTextLog.println("File being opened: " + aFileNames[1]);
          //Line counter variable
          int tWordCounter = 0;
          //Word counter variable set at -1 to account for eof
          int tLineCounter = -1;
          //Recognises punctuation
          tStreamTokens.wordChars('.', '.');
          tStreamTokens.wordChars(',', ',');
          tStreamTokens.wordChars('\'', '\'');
          tStreamTokens.eolIsSignificant(true);
          //while there are more lines of text in the file
          while(tStreamTokens.nextToken() != StreamTokenizer.TT_EOF)
               //if the stream token is a string
               if(tStreamTokens.ttype == StreamTokenizer.TT_WORD)
                    //and if the token value is "apples"
                    if(tStreamTokens.sval.equals("apples"))
                         //change the stream token to "Apples"
                         tStreamTokens.sval = ("Apples");
                         tTextLog.println("Word changed at Line " + tStreamTokens.lineno());
                    //increment the word counter
                    tWordCounter++;
               //if the end of the line has been reached, increment the line counter
               else if(tStreamTokens.ttype == StreamTokenizer.TT_EOL)
                    tStreamTokens.sval = ("");
                    tTextOut.println("");
                    tLineCounter ++;
                    tTextLog.print("End of line reached. Word Count: " + tWordCounter + " . ");
                    tTextLog.println("Line Count: " + tLineCounter);
               //writes the current token to the output file named in the command line
               tTextOut.print(tStreamTokens.sval + " ");
          //prints the values to the designated output file
          tTextOut.println("Name of input file: " + aFileNames[0]);
          tTextOut.println("Number of lines: " + tLineCounter);
          tTextOut.println("Number of words: " + tWordCounter);
          //prints the values to the command prompt
          System.out.println("Name of input file: " + aFileNames[0]);
          System.out.println("Number of lines: " + tLineCounter);
          System.out.println("Number of words: " + tWordCounter);
          //prints the values to the log file
          tTextLog.println("Total Number of Lines: " + tLineCounter);
          tTextLog.println("Total Number of Words: " + tWordCounter);
          //Writes name of files being closed to log file
          tTextLog.println("File being closed: " + aFileNames[0]);
          tTextLog.println("File being closed: " + aFileNames[1]);
          //Closes the files
          tTextIn.close();
          tTextOut.close();
          tTextLog.close();
}Apologies for the not-very-nice code. I'd be grateful for any help on this.
C

for one, you might want it to check for "apples."Thanks for your help! I thought I was already
checking, here:
//and if the token value is "apples"
     if(tStreamTokens.sval.equals("apples")) ///<--here
          //change the stream token to "Apples"
          tStreamTokens.sval = ("Apples");
tTextLog.println("Word changed at Line " +
+  tStreamTokens.lineno());
     }but I'll have to look into it some more.
C
maybe you should read the post again. you need to check for the period too, dork.
     if(tStreamTokens.sval.equals("apples")) {     
     //change the stream token to "Apples"
             tStreamTokens.sval = ("Apples");
          tTextLog.println("Word changed at Line " +  tStreamTokens.lineno());
     }should be something like
     if(tStreamTokens.sval.equals("apples")) {     
     //change the stream token to "Apples"
             tStreamTokens.sval = ("Apples");
          tTextLog.println("Word changed at Line " +  tStreamTokens.lineno());
        }else if(tStreamTokens.sval.equals("apples.")) {     
     //checking for "apples." also
             tStreamTokens.sval = ("Apples.");
          tTextLog.println("Word changed at Line " +  tStreamTokens.lineno());

Similar Messages

  • Has anyone else had an iPhone 5 that's only been dropped once accidentally and smashed the screen? Mine has done that and as I don't have mobile insurance it'll cost a fortune to replace it through Apple!!

    Has anyone else had an iPhone 5 that's only been dropped once accidentally and smashed the screen? Mine has done that and as I don't have mobile insurance it'll cost a fortune to replace it through Apple!!  I don't think the phone is fit for purpose as any other phone i've had has been dropped and been fine and surely they expect a mobile to be accidentally dropped at least once!!

    Yeah, sad to hear of your accident:
    If you dropped it on concrete- that is the hardest material and chance of damage goes up tremedously.  Actually you are fortunate not to have mobile insurance because then you have to trust your insurance provider to provide you with a quality replacement or repair.
    Apple is always spot-on you know what you're getting.  They can always attempt to replace JUST the display on an iPhone 5 for $149 + tax is what I've heard them quote others. But they said it's $229 to replace your phone with another whole new iPhone 5.  Get the AppleCare+ because two of these incidents will only cost you $49 + tax in the future!

  • "Replace letters with" function

    How to tell Mail to use "Replace letters with" function of Keyboard/text? All other apps are using this!
    Thanks,
    Matyas

    you can create your own replaceString function;
    i.e.
    http://www.worlddeveloper.org/www/forumtopicview.html?fid=167&categoryId=24&fpn=0

  • Email Messages Replacing Letters With = Signs

    Somebody please help I am having an issue with my z10. When I send an email from my device is adds a bunch of script into the email that should not be there. It even goes as far as replacing letters I have put in with = signs and a bunch of other random stuff. Such as =/div>, =/body>, rev&n=sp. I am not sure what is going on here but it is really messing up my emails. Can someone help?

    Same here. In my case the same email when read on another smartphone does not contain these artifacts. The BB is obviously mis-encoding something that our email clients (Outlook in my case) are sensitive to.
    What about you? Trashed only in Outlook or something like that, or also when read back on another mobile device?

  • Adobe Pro XI deletes and/or replaces letters with blank spaces or an x

    How do I fix the problem where Adobe Pro XI deletes and/or replaces letters with blank spaces or an x.
    This is really annoying to have to go back to each thing I type into a document and re-choose the fonts to get them to be correct.
    Thanks for your help!

    Adobe is a company, not a program... do you mean ACROBAT Pro?
    If yes, this is the Premiere Pro video editing forum, you need to go to http://forums.adobe.com/community/acrobat

  • URGENT : replacing letters in an expression

    Hi all
    I think that I should post this topic here because I think it concerns SQL functions. Here is the problem :
    I am developping a forms application . There is a listbox named FORMULA containing list of formulas. When I choose a formula from that list then there is a program which exctracts all of its operands. For example if the formula is : (a+b)*abs(c-d) then the operands are : a,b,c and d.
    Then I populate another listbox named OPERANDS with these operands. So the value of the OPERANDS listbox is either a or b or c or d in our example.
    I held the expression of the formula in a global variable so it can be accessible anywhere in my application.
    When I choose one of the operands from the OPERANDS listbox , for example b,then I enter its value , for example 5, in a text field named VALUE ,and then I click on a button to replace the letter "b" in the formula with the value I entered , which is 5. So I want to get an expression like this : (a+5)*abs(c-d) .
    But the problem is that when I run the application then all letters "b" in the formula are replaced by 5 . So even the expression "abs", which is a pl/sql function, is replaced by "a5s". And I do not want that to happen.
    Here is how I proceeded :
    formula := replace(formula,operands,value); where "formula" is the global variable containing the formula expression, "operands" is the variable containing the value of the OPERANDS listbox which is "b" here, and "value" is the variable containing the value 5.
    So how can I avoid this misreplacement ?
    Thank you very much indeed.

    post it where you want, but only once...
    I will follow the other thread
    URGENT : replacing some letters in an expression

  • My iphone 5s 64gb golden colour is broken purchased from uae ..I want to replace it through company by paying extra cost ..I have pics of my broken iphone anyone can help me here?? if apple guys want to see the pics of the particular phone to understand..

    My  iphone 5s 64gb golden colour is broken and I want to replace it by paying extra money. I have pics of my broken phone if apple guys want to see the condition of phone than where should I send those pics...one more thing I lost the bill..and it is purchased in uae...

    It can only be serviced / replaced in the country where it was originally sold. All service and support in UAE is provided by the authorized carriers there. Take it in to the carrier it was purchased from for service.

  • Letters through PB60

    Hi,
    I have created an interview letter in SO10 and have used PB60 to display this letter. But now I want some extra fields like Date and Time of the interview in my letter. These fields are in my custom infotype 9XXX.
    When I try to use Insert-----> Program symbols in the letter through SO10, I dont find my 9XXX infotype in it. Due to this I can't insert these fields. What can be done in order to get these fields in my letter? Plese help.
    Thanks and Regards,
    Anu.

    I have solved the issue. Have created two nes fields in my custom infotype and have used the user exit MPAP3CUS by copying it to ZPAP3CUS. This is Standard copy for user-exit for creating text variables in SO10. For more information check the documentation in SPRO
    Personal Management -
    > Recruitment-> Applicant Selection->Applicant Activities>Applicant Correspondence>Enhancement: Create new Text Variables

  • Replacement Phone through Asurion

    So besides the fact that it took 3 weeks to get a replacement phone, I just got it today and sent my old one back in the mail. I am a bit concerned because I clicked to reset all settings not reset all CONTENT and settings so I am worried they will charge me!

    Did you turn off Find My iPhone first? If you turned off find my iPhone and then did the reset all settings, and they can get on the phone without a passcode, you should be ok. They do try to restore these phones and use them as replacements.
    Agree with Loquat, I would be concerned you left your personal information intact.

  • Help me. my ipod replaced music through TV broadcasts!

    I want to put Broadcasts on my iPod , but iTunes will replace than the entire music. Is there the possibility that music and broadcasts are at the same time on my iPod! I really need Help, thank you!
    i'm from German, so my Englisch is not really good. I'm sorry

    It sounds like yu are trying to sync the Bradcast app from a computer other than the computer yu sync yu iPod with.  You can only sync with one iTunes library.
    Why not download/install the app directly to the iPod?

  • Accessing Modified Letters through Keyboard

    How do I enable the option to get modified letters by holding down my keyboard letter?
    I used to be able to type a modified letter (Accented letters, etc) by just holding down the letter it corresponds to, but since I updated my OS, this is not possible anymore. I've looked into my Keyboard preferences, but couldn't find the right option to re-enable this.
    HELP!!

    How do I enable the option to get modified letters by holding down my keyboard letter?
    I used to be able to type a modified letter (Accented letters, etc) by just holding down the letter it corresponds to, but since I updated my OS, this is not possible anymore. I've looked into my Keyboard preferences, but couldn't find the right option to re-enable this.
    HELP!!

  • Has anyone received a replacement ipod through the replacement programme yet?

    Hi, i sent my ipod to apple over a month ago. replacement not received yet. has anyone received anything? is this delay normal?
    thank you

    Hi Everyone. I have one black 1g Ipod nano working perfectly the only problem is that only one quarter of the screen work but I still using like that. What happend with this? Can I change it in the replacment program?....
    Thanks

  • Foot stand not provided with my replacement WRT-350N - need help with Cisco contact

    Hi!
    I am starting to feel like Michael Douglas in the movie Falling Down and need some help.
    Story:
    I finally sent in my faulty WRT-350N router and when I got the replacement everything but the plastic foot stand was included. I want to have my router standing up to save desk space but now I have no foot.
    "OK, should not be hard to get Linksys to send me the missing foot stand" was my thought. Now I have called the RMA line and also emailed them and I get a similar answer like Michael Douglas got with a smile
    I keep hearing that I cannot get the part since it is not on the product's content list. Like that is *my* problem. I just want the part and do not care whether it is on a list or not. It is the part on top of the router in the picture. I even asked the Linksys representative to Google a bit for WRT-350N and there are foot stands on almost all pictures and it is definitely included in the box. I was told I could go nowhere else for help either with this. I really doubt that but fail to find a channel to Linksys that may be able to help.
    If some Linksys representative sees this please help me!
    Thanks, Niklas
    RMA XXXXX - missing router stand/foot
    (Mod note: Edited for guideline compliance. E-mail conversation removed.)
    Message Edited by kent07 on 07-01-2009 03:10 AM
    Solved!
    Go to Solution.

    Now it should be working. To moderators, this does not hold any personal info but just want to show the quite long text to read through for the RMA:
    Thank you for contacting Linksys Customer Service Department. First of all, we would like to inform you that, if your item has been purchased less than 2 years ago, you have the possibility of replacing it through the place of purchase.
    If you want to replace your item through Linksys, we are more than glad to provide you with our assistance. In order to create an RMA ( authorization number for the replacement under warranty), you can do it online at
    https://linksysrma.moduslink.com/Consumer/pag/ChooseRegion.aspx.
    On the other hand, if you prefer us placing the RMA, please reply to this email including the following information:
    Name:
    Last Name:
    Company Name:
    Street Address:
    City:
    Postal Code:
    Day Time Phone Number:
    Model (include version):
    Serial Number:
    Date of Purchase:
    Place of Purchase (store):
    Once the RMA is created, you will receive a confirmation e-mail with the RMA number on a shipping label ( not a prepaid, as the inbound shipping is up to the customer) and all the terms and conditions . You will have to print three copies of that label. The first copy you are going to stick it in the outside of the box, the second one you are going to join it in the inside of the box and the last one is a copy for you as assurance. Together with this e-mail it will also be included all the terms and conditions. We will strongly appreciate your reading them carefully before sending the item to us. In relation to this, it will be our pleasure to summarize some of the important conditions we state in order to clarify our standard procedure:
    Please write down the RMA# ( not the case id#) on the outside of the box with big numbers and letters. Moreover, we recommend our customers to use a traceable shipping method in order to get a tracking number for the delivery. This number will be helpful to track the package in case of any potential inconvenience. Remember that you will have to pay just for the inbound shipping while Linksys will be responsible for the rest of the expenses.
    Furthermore, remember to include all the accessories that were included in the original package: Power Supply, Cables, User Guide, CD's. Otherwise, warranty replacement will not be possible to be performed. All other accessories shipped that did not come in the original box may not be returned. Moreover, it is important to remember that you do not have to ship the original box. Also, include on the shipping box a copy of the proof of purchase, not the original.
    IMPORTANT: -If your product is part of a network kit just send the defective unit.
    If you have further questions do not hesitate to call us or replying to this e-mail. Our lines are open from Monday to Friday from 10 a.m. to 7 p.m. We recommend our customers to have the case id# handy.
    Regards,
    Linksys Customer Service - EMEA
    Linksys does NOT offer refunds, substitutions, credits, or upgrades.
    Linksys is NOT responsible for lost packages in transit. Please obtain a tracking number as a safeguard for your shipment. Linksys strongly suggests using a reputable shipping company that will provide a tracking number and will insure the package.
    Linksys is not able to accommodate walk-in customers.
    Processing the Defective Unit.
    Package your return unit(s) in one box, please make sure the contents are secure and that enough packaging material is included to prevent the unit(s) from moving around during shipping.
    Please ship the defective unit(s) to the address below.
    Linksys RMA/SILS/ML
    IJsseldijk 29 in Apeldoorn
    7325 WZ Apeldoorn
    The Netherlands
    All original Linksys accessories, such as power adapters, couplers/dongles, and antennas MUST BE RETURNED with the product.
    If you are replacing a product that belongs to a networking kit, only return the defective product listed on the previous web form belonging to the networking kit. Please do not include the working product.
    Linksys is NOT responsible for lost packages in transit. Please obtain a tracking number as a safeguard for your shipment. Linksys strongly suggests using a reputable shipping company that will provide a tracking number and will insure the package.
    Linksys is NOT responsible for lost or damaged personal accessories. If you have attached any accessories that did not come with the original product, please remove these items prior to returning your unit(s). In addition, if your product uses any internal fiber modules please also remove these items unless they are listed as one of the products on the RMA web form.
    Linksys is not responsible for data stored on the hard drive of the defective unit. If the defective unit has a hard drive and it cannot easily be removed form the defective unit, backup your data prior to shipping the defective unit to Linksys.
    The customer bears the cost of sending the defective unit(s) to Linksys including all customs fees and applicable taxes.
    A COPY of the proof of purchase must be included for all products. A valid proof of purchase includes a copy of the receipt, invoice, or packing slip from the retailer, or distributor. A copy of your credit card statement, internal requisition, or purchase order is NOT considered a valid proof of purchase. DO NOT SEND YOUR ORIGINALS.
    Processing the Replacement Unit.
    Linksys bears the cost of shipping the replacement unit to the CUSTOMER unless the expedited shipping option is chosen at the time the RMA is created.
    Upon receipt of the defective unit, a replacement unit is usually shipped within 3-5 business days
    Linksys ships replacement products via GLS ground to customers within the European Union. Non European Union Customer's replacements are shipped FedEx.
    In the event of a backorder, units will ship when available, and the shipping method will remain the same as the option selected at the time the RMA was created.
    If you wish to change the shipping method, YOU MUST contact the Linksys Customer Service department prior to the replacement unit being shipped. Once the unit has been shipped, the funds paid for expedited shipping cannot be refunded. The Customer Service phone numbers are listed below.
    Once you receive your replacement unit, test the unit, and verify that it is working properly.
    Damaged or missing part(s) must be reported within five business days of receiving the replacement unit(s).
    IMPORTANT: If your unit uses a power supply, make sure to use the power supply that comes with the replacement unit, and not the original power supply.
    COUNTRY
    PHONE NUMBER
    Austria
    01360 2772061
    Belgium
    02 627 7077
    Czech Republic
    800 800156
    Denmark
    82 332729
    Finland
    0800 523062
    France
    0800 881 026
    Germany
    0800 1013311
    Hungary
    06 80 204 548
    Iceland
    44 207 660 0121
    Ireland
    1 800 818 188
    Italy
    02 38 591012
    Lithuania
    44 207 660 0121
    Luxembourg
    32 2-627-7077
    Malta
    44 207 660 0121
    Netherlands
    0800 020 0101
    Norway
    235 00060
    Poland
    00800 331 1345
    Portugal
    213 180 081
    Spain
    900 902 207
    Sweden
    0851 992 251
    Switzerland
    022 5675 330
    Turkey
    212 444 2726
    United Kingdom
    0800 026 1418

  • How to activate a new phone to replace a broken one

    I have a family plan, with 4 phones on it.
    My daughter put hers through the washing machine, its toast!
    So, we have a brand new, never-used palm center on hand.
    But when I try to activate it, using the page at https://www.verizonwireless.com/b2c/activation/activatephone.jsp I pick the line, enter the new MEID and then it just takes me to a page with no other options, nothing happens!

    ok, so I eventually find my way to the number 800-922-0204
    I call that, walk through the menus.
    it tells me turn off the old phone, dial *228-SEND from the new one and walk through the activation
    well, the old one is toast, its definitely off, so I follow along
    - dial *228-send
    - enter the phone-line # I want the new phone on
    - enter my billing passwd, replacing letters with digits
    - it gives me a "wait a minute message"
    - then it gives me a "we cannot activate your equipment" message, no reason
    - then it tells me to dial 800-922-0204 for assistance
    so, here I am, stuck in an endless loop!

  • Looking for a replacement battery for dap-hd0

    I have a zen 20gb (dap-hd005) i have had it for aprox 8 months and the battery has now basicly stopped working all together. when it is unplugged it only lasts for about 5mins. the creative web site doesn't help they don't even have this model on there anymore.is there anyone who knows where i could a buy replacement battery??thanks danib747

    finally got around to getting the battery out of the player and found a replacement battery through yankee roo
    https://www.yankeeroo.com/store/product.php?productid=370&cat=0&page=
    i ended up winning a new zen through a competition so i never bought the battery for it
    lol
    danib747

Maybe you are looking for