• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

reverse the text file and write

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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

 
Ranch Hand
Posts: 112
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 198
Oracle Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Author
Posts: 116
11
Spring Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.

 
Marshal
Posts: 80745
485
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 80745
485
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I see the example you gave reverses “Ye - es” as two words.
 
Greenhorn
Posts: 7
Android Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@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
Marshal
Posts: 80745
485
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 198
Oracle Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Manoj Kumar Jain

how am i suppose to call the 2nd line of text? and so on?
 
Campbell Ritchie
Marshal
Posts: 80745
485
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
    Posts: 20
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    using the hint given by Manoj Kumar Jain

    i gt it



    but i don't like it,

    i want to use tokenizer please....
     
    Bartender
    Posts: 10780
    71
    Hibernate Eclipse IDE Ubuntu
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    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
     
    brando brandido
    Greenhorn
    Posts: 20
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    yes but our teacher wants us to use stringtokenizer.

    so i must use it.
     
    Sheriff
    Posts: 22850
    132
    Eclipse IDE Spring Chrome Java Windows
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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:
     
    Winston Gutkowski
    Bartender
    Posts: 10780
    71
    Hibernate Eclipse IDE Ubuntu
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    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
     
    Ranch Hand
    Posts: 37
    Netbeans IDE Eclipse IDE Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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!
     
    Manoj Kumar Jain
    Ranch Hand
    Posts: 198
    Oracle Java Linux
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    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
    Posts: 198
    Oracle Java Linux
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    oh got it ! Actually I type that line myself and completed it by editing two times.... but code was working fine....
     
    Bartender
    Posts: 6109
    6
    Android IntelliJ IDE Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    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
    Posts: 198
    Oracle Java Linux
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    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
    Posts: 198
    Oracle Java Linux
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    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
    Posts: 10780
    71
    Hibernate Eclipse IDE Ubuntu
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    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
    Posts: 198
    Oracle Java Linux
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    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
    Posts: 6109
    6
    Android IntelliJ IDE Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    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
    Posts: 198
    Oracle Java Linux
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    oh, so he is playing with words...
    anyways, I hope my english will also improve along with JAVA here
     
    Greenhorn
    Posts: 5
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    hope this one will help..



    Just edit this code if you like.. hehe
     
    Jeff Verdegan
    Bartender
    Posts: 6109
    6
    Android IntelliJ IDE Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    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
    Posts: 20
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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.

     
    With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
    reply
      Bookmark Topic Watch Topic
    • New Topic