aspose file tools*
The moose likes Beginning Java and the fly likes String reversal program Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "String reversal program" Watch "String reversal program" New topic
Author

String reversal program

Vladislav Mkrtychev
Greenhorn

Joined: Nov 29, 2008
Posts: 8
I am stuck on this project that is supposed to accept a text file as an input, reverse the text (ex: Hello there!....!ereht olleH) and then output to another file. So if I run this program using the second file as an input, then text comes back to normal. There are two classes - ReverseLine and a ReverseLineTester. I run it from the CLI (Ex: >ReverseLine File_1)
For now it does not work. Any advise? Thanks!




fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11230
    
  16

"it does not work" doesn't tell us much. Does it compile? Does it run then error out? Does it run, but produce no output? the wrong output?

The more details you can give, the better chances are someone will help you.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Vladislav Mkrtychev
Greenhorn

Joined: Nov 29, 2008
Posts: 8
Sorry, it compiles, runs and does not create the file with reversed text.
Does not create anything!
[ November 29, 2008: Message edited by: Vladislav Mkrtychev ]
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38363
    
  23
When I tried it, it compiled, ran and created File_2.txt with text identical to the original.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38363
    
  23
By the way, since this is obviously a class exercise, I think you are not supposed to use a StringBuilder to reverse the String. I think you are supposed to use the String method which creates a char[] array, then create a reverseArray(char[]) method, probably using a swapPair(char[], int, int) method.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19670
    
  18

Originally posted by Vladislav Mkrtychev:
rn.reverseLine(line);

That call does not modify the contents of the variable line; I think you mean

Remember,
a) String objects are immutable
b) Java uses pass-by-value for parameters


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Vladislav Mkrtychev
Greenhorn

Joined: Nov 29, 2008
Posts: 8
Campbell Richie - could you tell me if the file created is in the same directory? Because I've tried looking for that File_2 and could not find it... Yes it is a class project and we are covering File I/O, so we are encouraged to use all things related to it.

Rob Prime - Thanks for the tip! I will try that.

Thanks all!
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38363
    
  23
Have you come across the java.util.Scanner and java.util.Formatter classes?
Yes, the files were in the same directory.
Vladislav Mkrtychev
Greenhorn

Joined: Nov 29, 2008
Posts: 8
Yes, I am somewhat familiar with the Scanner class,
however, I have never dealt with the Formatter...
Just ran the program...
Still can't find the second file
Vladislav Mkrtychev
Greenhorn

Joined: Nov 29, 2008
Posts: 8
Wait a minute...
When I run the program and give it File_1 as an argument - nothing happens.
(It runs without any errors but does not do anything either)
When I run it and give it File_1.txt as an argument, it complains "Invalid variable declaration"
Olivier Legat
Ranch Hand

Joined: Nov 17, 2007
Posts: 176

Well this is certainly awkard because I get "File not found!" no matter what I put as argument. Oh well, it's not the first time I've come across a problem with the FileReader class.


Olly
Vladislav Mkrtychev
Greenhorn

Joined: Nov 29, 2008
Posts: 8
That's probably because you don't have that file?
 
jQuery in Action, 2nd edition
 
subject: String reversal program