-
String Conversion....
Hello,
I am stuck on this project that is supposed to accept a test 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 have tried to compile it and I get the error message:
1 error found:
File: C:\Documents and Settings\desktop\Java Excercises\Reverse Line\ReverseLineTester.java [line: 26]
Error: C:\Documents and Settings\desktop\Java Excercises\Reverse Line\ReverseLineTester.java:26: cannot find symbol
symbol : method reverseLine(java.lang.String)
location: class ReverseLineTester
Here is what I have so far:
Code:
import java.io.FileReader;
import java.io.PrintWriter;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class ReverseLineTester
{
public static void main(String[] args)
{
try
{
for (int i = 1; i < args.length; i++)
{
FileReader reader = new FileReader(args[i]);
Scanner in = new Scanner(reader);
PrintWriter out = new PrintWriter("File_2.txt");
int lineNumber = 1;
while (in.hasNextLine())
{
String line = in.nextLine();
System.out.print(reverseLine(line));
lineNumber++;
}
}
}
catch (FileNotFoundException ex)
{
System.out.print("File not found!");
}
}
}
Code:
public class ReverseLine
{
private String line;
public ReverseLine()
{
}
public ReverseLine(String myline)
{
line = myline;
}
public String reverseLine(String str)
{
StringBuffer buffer = new StringBuffer(str);
buffer = buffer.reverse();
String reverseString = buffer.toString();
return reverseString;
}
}
-
Look at this statement,
Code:
System.out.print(reverseLine(line));
You are referring that method in wrong way. reverseLine() method is not in the ReverseLineTester class, it's on other class. To access that you need an instance to the class.
-
Questions ...
- What is the number of arguments that your program is receiving? Because if it's just one input file name, then nothing is going to happen:
Code:
for (int i = 1; i < args.length; i++)
The compare condition would be 1<1 and the for loop would not execute. To avoid this, change the initial variable assignment to int i = 0.
- Another question... what is the for loop for?
Luck,
CJSL
-
Oh, man, I see it now!
Thanks!
How is my ReverseLine class looking?
-
Yes, that's true. I don't think I need a loop here.
Program supposed to run from the CLI (ex: ReverseLineTester File_1)
So I probably should get rid of "for" line and change
FileReader reader = new FileReader(args[i]);
to
FileReader reader = new FileReader(args[1]);
-
index based
Remember, arrays are index based, which means that their first element is 0 (args is nothing but an array). Therefore if you only have one argument it should be referenced as args[0], not args[1] (which would be the second element of the array).
Luck,
CJSL
-
<CODE>import java.io.FileReader;
import java.io.PrintWriter;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class ReverseLineTester
{
public static void main(String[] args)
{
try
{
String rline = "";
FileReader fileIn = new FileReader(args[0]);
Scanner in = new Scanner(fileIn);
PrintWriter fileOut = new PrintWriter("File_2.txt");
ReverseLine rn = new ReverseLine(rline);
int lineNumber = 1;
while (in.hasNextLine())
{
String line = in.nextLine();
rn.reverseLine(line);
fileOut.print(line);
lineNumber++;
}
}
catch (FileNotFoundException ex)
{
System.out.print("File not found!");
}
}
}</CODE>
Is this the way to go?
-
Does it work?
I'm assuming that you haven't finished the program because you have a variable, lineNumber, that's defined (wrongly defined) but isn't used. So... the questions is:
- Does the program work? ...does it do what you want it to do?
CJSL
-
Your assumptions are correct - it does not.
It compiles, but does not create the second file with the text backwards (File_2.txt)
I thought lineNumber is sort of a counter to go through the file line by line...
Could you also tell me if ReverseLine class is all right?
Thanks!