Forums Register Login

reverse the text file and write

+Pie Number of slices to send: Send
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

+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
 

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
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
 

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.

 
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
I see the example you gave reverses “Ye - es” as two words.
+Pie Number of slices to send: Send
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)
+Pie Number of slices to send: Send
 

Pablo Reyes wrote:it should work


No it won't for the reason already pointed out earlier in this thread by Manoj Kumar Jain
+Pie Number of slices to send: Send
@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.

+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
@Manoj Kumar Jain

how am i suppose to call the 2nd line of text? and so on?
+Pie Number of slices to send: Send
 

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.
    +Pie Number of slices to send: Send
    using the hint given by Manoj Kumar Jain

    i gt it



    but i don't like it,

    i want to use tokenizer please....
    +Pie Number of slices to send: Send
     

    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
    +Pie Number of slices to send: Send
    yes but our teacher wants us to use stringtokenizer.

    so i must use it.
    +Pie Number of slices to send: Send
    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:
    +Pie Number of slices to send: Send
     

    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
    +Pie Number of slices to send: Send
    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!
    +Pie Number of slices to send: Send
     

    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..
    +Pie Number of slices to send: Send
    oh got it ! Actually I type that line myself and completed it by editing two times.... but code was working fine....
    +Pie Number of slices to send: Send
     

    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?
    +Pie Number of slices to send: Send
     

    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.
    +Pie Number of slices to send: Send
     

    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..
    +Pie Number of slices to send: Send
     

    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
    +Pie Number of slices to send: Send
     

    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 ?
    +Pie Number of slices to send: Send
     

    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.
    +Pie Number of slices to send: Send
    oh, so he is playing with words...
    anyways, I hope my english will also improve along with JAVA here
    +Pie Number of slices to send: Send
    hope this one will help..



    Just edit this code if you like.. hehe
    +Pie Number of slices to send: Send
     

    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."
    +Pie Number of slices to send: Send
    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.

    There’s no place like 127.0.0.1. But I'll always remember this tiny ad:
    a bit of art, as a gift, the permaculture playing cards
    https://gardener-gift.com


    reply
    reply
    This thread has been viewed 23931 times.
    Similar Threads
    FileWrite Problem
    Why no output is given here
    Remove Line from Text File
    I cannot figure out how to put on JFrame reverse the txt file put on text area and write on other
    FileIO
    More...

    All times above are in ranch (not your local) time.
    The current ranch time is
    Mar 28, 2024 14:31:40.