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
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:
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.
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)
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.
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.
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.
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 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().
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:
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.
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. .
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 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.
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."