Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Beginning Java and the fly likes reverse the text file and write Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "reverse the text file and write" Watch "reverse the text file and write" New topic
Author

reverse the text file and write

brando brandido
Greenhorn

Joined: Jan 11, 2012
Posts: 20
hello i am having trouble how to reverse the given text file word by word.

this is a sample text file



need to display the text file and write it in a new file in reverse word by word.

so give the about input

the new file in reverse will look like this



this is what i got so far, i am only able to display the text file

Pavan Kumar Dittakavi
Ranch Hand

Joined: Feb 12, 2011
Posts: 104

You are almost there. Just reverse the string that you have got into the 'data' string object and write it to the file.

Thanks,
Pavan.
Manoj Kumar Jain
Ranch Hand

Joined: Aug 22, 2008
Posts: 191

Pavan Kumar Dittakavi wrote:You are almost there. Just reverse the string that you have got into the 'data' string object and write it to the file.


No, the problem doesn't say that he need to reverse the complete line, rather the problem is that he need to reverse the each word.
He can do it by parsing the line by whitespace and then reverse each word but he should take care of this
"far," should be revresed like "raf," not ",raf"


so he need to take care of punctuation marks as well.

I would like to suggest you to parse the line with whitespace that you are getting in "data" this will give you the list of words in a single line.
now process each word in the list to be reversed, you need to avoid the (, . ? ... etc) if these are appearing in the end of the word


Do not wait to strike till the iron is hot; but make it hot by striking....
Alex Theedom
Ranch Hand

Joined: Jan 18, 2012
Posts: 48

A possible solution is to read a line of text from the file, use a regular expression to match on a word, reverse the word and write back to the file. Here is my solution:



As you cannot see line 18 contains a . . .

I hope this helps.


Alex Theedom - Senior Java EE Developer (OCPJP/OCPJWCD)
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3429
    
  12
Alex Theedom wrote:I hope this helps.

Actually it doesn't help at all. As it says at the top of the Beginner's Forum
We're all here to learn, so when responding to others, please focus on helping them discover their own solutions, instead of simply providing answers.


Joanne
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38039
    
  22
Pencil and paper. Write down how you intend to identify words, and how to print them backwards. I think “Ye - es” will be almost impossible to identify as a word.
Once you have got it written down, and reduced it to words of one syllable, you will find it very easy ot convert to code.

And thank you for noticing the straight answer, Joanne. I have altered it slightly.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38039
    
  22
I see the example you gave reverses “Ye - es” as two words.
Pablo Reyes
Greenhorn

Joined: Oct 22, 2011
Posts: 7

When you have the line read in data, split it with the split() method using a space as a separator. Then, loop through the array and reverse each element and print it.



Maybe not the most efficient solution, but it should work (haven't tested it)
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3429
    
  12
Pablo Reyes wrote:it should work

No it won't for the reason already pointed out earlier in this thread by Manoj Kumar Jain
brando brandido
Greenhorn

Joined: Jan 11, 2012
Posts: 20
@Pablo Reyes

it wont work because you will end up with word like this .se-eY" the right reverse should look like this “eY – se.

this is what i did



i am able to read the first line only, i dont know how to get the next text line

the output of the above code is


my plan is to pick each word and arrange them later to get the desired output in the reverse_output.txt file
so i will pick the first two word and insert - between them, and " before them. and the the rest too.

i probably will use indexOf and substring? but don't know where to start, need idea or example to proceed.

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38039
    
  22
Still not quite right; you are missing out the punctuation.

I think you have been given a task far too difficult for your level of experience. I think you would use a regular expression and print what was matched and reverse what was skipped. But I can’t think offhand what the code would look like.
Manoj Kumar Jain
Ranch Hand

Joined: Aug 22, 2008
Posts: 191

I can think of one of the solution to this problem.
As I said earlier first you need to parse the complete line with space to get a list of the words.
now take each word and process it character by character.
holds true then add the characters to an string and reverse them till you find a special char.
I am not able to explain it very well let me paste some sample code here.


This seems fine to me...
if you want to add spaces at both side of - symbol you can add that too.
Hope I am not violating rules and Campbell will not delete the code.
brando brandido
Greenhorn

Joined: Jan 11, 2012
Posts: 20
@Manoj Kumar Jain

how am i suppose to call the 2nd line of text? and so on?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38039
    
  22
Manoj Kumar Jain wrote: . . . . . .
Good grief! There are at least 4 errors in that line alone!
  • 1: It won’t compile.
  • 2: Poor formatting; you should have spaces around all binary operators.
  • 3: Minor error: use of decimal numbers rather than hex.
  • 4: Use of number literals at all. If you must use literals, why don’t you use character literals?
  • 5: Not looking in the Character class where you would have found a method which does all that for you.
  • So that’s 5

    And why do you need to look for spaces if you use that technique?

    Actually, if you are going to use that technique, you can put the individual letters into a StringBuilder in the appropriate location. Remember which characters require you to increase your insertion index and which don’t. Much easier.
    For a second line, you get your file reading classes to read the next line. Just append String lineEnd = System.getProperty("line.separator") at each line and increase your index by lineEnd.length(). Don’t use "\n" or similar. I think I have spelt the name of the property correctly; you will have to check that you get new lines rather than “null” printing out.
    brando brandido
    Greenhorn

    Joined: Jan 11, 2012
    Posts: 20
    using the hint given by Manoj Kumar Jain

    i gt it



    but i don't like it,

    i want to use tokenizer please....
    Winston Gutkowski
    Bartender

    Joined: Mar 17, 2011
    Posts: 7552
        
      18

    brando brandido wrote:i want to use tokenizer please....

    If you're talking about StringTokenizer, my advice is: DON'T. It is a legacy class and it's use, particularly in new code, is discouraged (have a look at the documentation).

    You can do exactly the same things (and more) with String.split().

    Winston


    Isn't it funny how there's always time and money enough to do it WRONG?
    Articles by Winston can be found here
    brando brandido
    Greenhorn

    Joined: Jan 11, 2012
    Posts: 20
    yes but our teacher wants us to use stringtokenizer.

    so i must use it.
    Rob Spoor
    Sheriff

    Joined: Oct 27, 2005
    Posts: 19654
        
      18

    In this case I think StringTokenizer is a better solution than String.split because you need the tokens you split on as well. With String.split these are discarded.

    An alternative I would use is directly use Pattern and Matcher. In semi pseudo code:


    SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
    How To Ask Questions How To Answer Questions
    Winston Gutkowski
    Bartender

    Joined: Mar 17, 2011
    Posts: 7552
        
      18

    brando brandido wrote:yes but our teacher wants us to use stringtokenizer.
    so i must use it.

    You're in a classroom, not the army. It may be worth pointing out the relevant paragraph in StringTokenizer to your teacher (it's near the end of the main section and starts with the phrase "StringTokenizer is a legacy class..."), and asking him/her whether it would be permissible to use String.split() or, as Rob suggests: Pattern and Matcher.

    It's quite possible that the curriculum was written a while ago, and that he/she is unaware of the new recommendations (although they're not that new).

    If I were your teacher, I might even give you some brownie points.

    Winston
    Amit Goda
    Ranch Hand

    Joined: Jan 21, 2012
    Posts: 37

    A possible way according to me is, split contents of variable data at each whitespace character. You'll get an Array. And now traverse through the each word in array and use charAt(int index) method of String to reverse the word and store it in a String. Similarly append all the words and your output will be ready. .

    Hope it helps!


    AMiT
    Manoj Kumar Jain
    Ranch Hand

    Joined: Aug 22, 2008
    Posts: 191

    Campbell Ritchie wrote:
    Manoj Kumar Jain wrote: . . . . . .
    Good grief! There are at least 4 errors in that line alone!
  • 1: It won’t compile.

  • This code is compiling and running fine.. I pasted code here after running it..
    Manoj Kumar Jain
    Ranch Hand

    Joined: Aug 22, 2008
    Posts: 191

    oh got it ! Actually I type that line myself and completed it by editing two times.... but code was working fine....
    Jeff Verdegan
    Bartender

    Joined: Jan 03, 2004
    Posts: 6109
        
        6

    Manoj Kumar Jain wrote:oh got it ! Actually I type that line myself and completed it by editing two times.... but code was working fine....


    Did you notice his other points, that had nothing to do with whether it compiles or runs?
    Manoj Kumar Jain
    Ranch Hand

    Joined: Aug 22, 2008
    Posts: 191

    Amit Goda wrote:A possible way according to me is, split contents of variable data at each whitespace character. You'll get an Array. And now traverse through the each word in array and use charAt(int index) method of String to reverse the word and store it in a String. Similarly append all the words and your output will be ready. .

    Hope it helps!


    Welcome to the Ranch !
    Please read the thread from OP. Requirement is quite different, its not just to reverse each word.
    Manoj Kumar Jain
    Ranch Hand

    Joined: Aug 22, 2008
    Posts: 191

    Jeff Verdegan wrote:
    Manoj Kumar Jain wrote:oh got it ! Actually I type that line myself and completed it by editing two times.... but code was working fine....


    Did you notice his other points, that had nothing to do with whether it compiles or runs?


    Yes Jeff I noticed others points as well. He is quite correct and these points should be imbibed..
    Winston Gutkowski
    Bartender

    Joined: Mar 17, 2011
    Posts: 7552
        
      18

    Manoj Kumar Jain wrote:Yes Jeff I noticed others points as well. He is quite correct and these points should be imbibed..

    I thought it was pints that should be imbibed.

    Winston
    Manoj Kumar Jain
    Ranch Hand

    Joined: Aug 22, 2008
    Posts: 191

    Winston Gutkowski wrote:
    Manoj Kumar Jain wrote:Yes Jeff I noticed others points as well. He is quite correct and these points should be imbibed..

    I thought it was pints that should be imbibed.

    Winston

    sorry, couldn't get you ?
    Jeff Verdegan
    Bartender

    Joined: Jan 03, 2004
    Posts: 6109
        
        6

    Manoj Kumar Jain wrote:
    Winston Gutkowski wrote:
    Manoj Kumar Jain wrote:Yes Jeff I noticed others points as well. He is quite correct and these points should be imbibed..

    I thought it was pints that should be imbibed.

    Winston

    sorry, couldn't get you ?


    "Imbibe" means drink. In American and British English, we don't talk about "imbibing points". On the other hand a pint (of beer), is perfect for imbibing. Pint, point. Only a one-letter difference.
    Manoj Kumar Jain
    Ranch Hand

    Joined: Aug 22, 2008
    Posts: 191

    oh, so he is playing with words...
    anyways, I hope my english will also improve along with JAVA here
    Ryan Alfeche
    Greenhorn

    Joined: Jan 16, 2012
    Posts: 5
    hope this one will help..



    Just edit this code if you like.. hehe
    Jeff Verdegan
    Bartender

    Joined: Jan 03, 2004
    Posts: 6109
        
        6

    Ryan Alfeche wrote:hope this one will help..


    No, it is not helpful. Handing somebody the answer rather than nudging them in the right direction so that they can figure it out for themselves simply denies them the opportunity to learn and encourages intellectual dishonesty.

    This site is NotACodeMill(⇐link), and as it says right on the topic page, "We're all here to learn, so when responding to others, please focus on helping them discover their own solutions, instead of simply providing answers."
    brando brandido
    Greenhorn

    Joined: Jan 11, 2012
    Posts: 20
    Thank you all for all the replies, suggestions and support

    i actually gt it too.

    This is my own version. with comments and proper programming.

     
    wood burning stoves
     
    subject: reverse the text file and write
     
    Similar Threads
    FileIO
    Java Writer, Java Filer, Tokenizer
    PrintStream & PrintWriter
    replacing content in a file
    Why no output is given here