• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

String reversal program

 
Vladislav Mkrtychev
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 12101
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"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.
 
Vladislav Mkrtychev
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When I tried it, it compiled, ran and created File_2.txt with text identical to the original.
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20514
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Vladislav Mkrtychev
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you come across the java.util.Scanner and java.util.Formatter classes?
Yes, the files were in the same directory.
 
Vladislav Mkrtychev
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 176
Chrome Mac Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Vladislav Mkrtychev
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's probably because you don't have that file?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic