Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Help Please - Rewriting text

  1. #1
    Junior Member
    Join Date
    Jan 2011
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Help Please - Rewriting text

    Hi everyone,
    I hope someone can help, I need to read a text file that the user inputs, the program then has to find words that appear more than 3 or 4 times and replace them with a shorthand version.
    so the following text:

    "hello, some times some people say hello and some times they don't, times sure are changing, say hello to the future"

    would be condensed and written to a new file as:

    "hello(h*), some(*s) times(t*) s* people say h* and s* t* they don't, t* sure are changing, say h* to the future"

    My idea was to take the input file, and add each string to an array. Then re-read the file, and check each word to see if it appears more than twice in the array. If it does then output is substring of the array concatenated with an asterix, otherwise just output the string as it appears. . . problem is I can't seem to implement my plan very effectively at all

    Any ideas?


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Help Please - Rewriting text

    Quote Originally Posted by dechno View Post
    problem is I can't seem to implement my plan very effectively at all
    Why not? What have you tried? What happened? What didn't work? Be specific, and feel free to post an SSCCE that demonstrates where you got stuck.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Jan 2011
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Help Please - Rewriting text

    Hi Kevin,
    First of all, I thought maybe I was over complicating the problem by using the method I mentioned above.
    The problem I seem to be having is the second loop is used to check each word and it's corresponding number of appearances before starting to write to the file
    Last edited by dechno; January 21st, 2011 at 06:15 PM. Reason: silly code

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Help Please - Rewriting text

    Well once you exit the first loop, you've gone through the whole file already, so in.hasNext() will return false.

    I would think that you should break this up into a couple different steps- the first step is to put all of the words into some kind of data structure (an array or a List, whatever). The second step is taking a List of words and converting the duplicates to the translated code words.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. The Following User Says Thank You to KevinWorkman For This Useful Post:

    dechno (January 21st, 2011)

  6. #5
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Help Please - Rewriting text

    Quote Originally Posted by dechno View Post
    Hi everyone,
    I hope someone can help, I need to read a text file that the user inputs, the program then has to find words that appear more than 3 or 4 times and replace them with a shorthand version.
    so the following text:

    "hello, some times some people say hello and some times they don't, times sure are changing, say hello to the future"

    would be condensed and written to a new file as:

    "hello(h*), some(*s) times(t*) s* people say h* and s* t* they don't, t* sure are changing, say h* to the future"

    My idea was to take the input file, and add each string to an array. Then re-read the file, and check each word to see if it appears more than twice in the array. If it does then output is substring of the array concatenated with an asterix, otherwise just output the string as it appears. . . problem is I can't seem to implement my plan very effectively at all

    Any ideas?
    The way you have it appear, it looks like the first occurrence, it's the whole word, but if again, then it's just the first character. If you can figure out how to figure which ones are more than twice, just change the 2nd and so on to that word . charAt(0) + "*".

  7. #6
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Help Please - Rewriting text

    Quote Originally Posted by javapenguin View Post
    The way you have it appear, it looks like the first occurrence, it's the whole word, but if again, then it's just the first character. If you can figure out how to figure which ones are more than twice, just change the 2nd and so on to that word . charAt(0) + "*".
    Once again, most of this information is useless. You basically repeated the OP's goals and then threw out the most trivial piece of the algorithm. Why do you keep doing stuff like this?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Text output in GUI
    By andreas81 in forum Java Theory & Questions
    Replies: 4
    Last Post: June 26th, 2013, 09:36 AM
  2. getting text from JEditorPane
    By nasi in forum AWT / Java Swing
    Replies: 1
    Last Post: May 18th, 2010, 07:38 AM
  3. java program to copy a text file to onother text file
    By francoc in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: April 23rd, 2010, 03:10 PM
  4. Coloured text
    By silverspoon34 in forum Java Theory & Questions
    Replies: 8
    Last Post: November 26th, 2009, 07:21 PM
  5. inputting of text
    By Subhasis Banerjee in forum JavaServer Pages: JSP & JSTL
    Replies: 3
    Last Post: October 30th, 2009, 12:53 PM