I am experimenting with the java language. By using some examples I
tried to create a program in which i can reverse the strings in a
file.
For example: Hello this is me --> em si siht olleH
So by studying on different pieces of java source code I mixed and
created a new program, it compiles without problems, but it doesn't
work.
Can someone explain me why it doesn't work and help me with a
solution?
source code:
import java.io.*;
import java.util.*;
import java.lang.*;
public class reverseStr9{
public void main(String args[]) throws Exception{
File file = new File(args[0]);
if(!file.exists() || !file.canRead()) {
System.out.println("Can't read " + file);
return;
}
else
try{
FileReader fr = new FileReader(file);
BufferedReader in = new BufferedReader(fr);
StringBuffer strTemp = new StringBuffer(in.readLine());
String newString =
strTemp.reverse().toString();
System.out.println(newString);
}
catch ( FileNotFoundException e ) {
System.out.println( "File Disappeared" );
}
}
}
text file name: piet.txt
Hello this is piet in the hottentottententoonstelling.
I hope that there is somebody who can answer my question.
greetings
Piet
> I am experimenting with the java language. By using some examples I
> tried to create a program in which i can reverse the strings in a
> file.
> For example: Hello this is me --> em si siht olleH
> So by studying on different pieces of java source code I mixed and
> created a new program, it compiles without problems, but it doesn't
> work.
You provide a nice code example, but
'it doesn't work' is not clear enough.
Please post the _exact_ error message
or Stacktrace you are getting.
--
Andrew Thompson
* http://www.PhySci.org/ Open-source software suite
* http://www.PhySci.org/codes/ Web & IT Help
* http://www.1point1C.org/ Science & Technology
"Winbatch" <winb...@techie.com> wrote in message news:<LKR3c.15570$c73.4...@twister.nyc.rr.com>...
for ( String line = in . readLine ( ) ; line != null ; line = in . readLine ( ) ) {
> > >
> > > StringBuffer strTemp = new StringBuffer(in.readLine());
> > > String newString =
> > > strTemp.reverse().toString();
> > > System.out.println(newString);
> > >
}
> > > }
> > >
> > > catch ( FileNotFoundException e ) {
> > > System.out.println( "File Disappeared" );
> > > }
> > > }
> > > }
> > >
> > > text file name: piet.txt
> > >
> > > Hello this is piet in the hottentottententoonstelling.
> > >
> > >
> > > I hope that there is somebody who can answer my question.
> > >
> > > greetings
> > >
> > > Piet
Use a for loop as shown above.
Hello Carla,
Thank you for your help
I tried to run the program it works, but not as expected.
I only changed reverseStr9 into reverseStr11
***************************************************
javac reverseStr11.java
java reverseStr11 piet.txt
result:
===========================================================
.noitseuq ym rewsna nac ohw ydobemos si ereht taht epoh I
Exception in thread "main" java.lang.NullPointerException
at java.lang.StringBuffer.<init>(StringBuffer.java:129)
at reverseStr11.main(reverseStr11.java:22)
==========================================================
content file: piet.txt
==========================================================
begin line
Hello this is piet in the hottentottententoonstelling.
I hope that there is somebody who can answer my question.
greetings
end line
==========================================================
I used your modified version.
used java source code:
==========================================================
import java.io.*;
import java.util.*;
//import java.lang.*;
public class reverseStr11{
public static void main(String args[]) throws Exception{
File file = new File(args[0]);
if(!file.exists() || !file.canRead()) {
System.out.println("Can't read " + file);
return;
}
else
try{
FileReader fr = new FileReader(file);
BufferedReader in = new BufferedReader(fr);
for ( String line = in.readLine() ; line != null ; line =
in.readLine()) {
StringBuffer strTemp = new StringBuffer(in.readLine());
String newString = strTemp.reverse().toString();
System.out.println(newString);
}
}
catch ( FileNotFoundException e ) {
System.out.println( "File Disappeared" );
}
}
}
==========================================================
I also tested the program with another textfile named piet2.txt and
here are the results:
content piet2.txt
==============================================
begin line
hallo dit is tekst van mij de piet2 DE ED ADE
fdasfhsadhkjfkjsadkfjakjdHF
import java.io.*;
import java.util.*;
import java.lang.*;
class reverseStr9{
public static void main(String args[]) throws Exception{
File file = new File(args[0]);
if(!file.exists() || !file.canRead()) {
System.out.println("Can't read " + file);
return;
}
else
try{
FileReader fr = new FileReader(file);
BufferedReader in = new BufferedReader(fr);
StringBuffer strTemp = new StringBuffer(in.readLine());
String newString = strTemp.reverse().toString();
System.out.println(newString);
}
catch ( FileNotFoundException e ) {
System.out.println( "File Disappeared" );
}
}
}
END LINE
============================================
result after typing in: java reverseStr11 piet2.txt
=========================================
EDA DE ED .......
;*.oi.avaj tropmi
:
:
;) "deraeppasiD eliF" (nltnirp.tuo.metsyS
}
Exception in thread "main" java.lang.NullPointerException
at java.lang.StringBuffer.<init>(StringBuffer.java:129)
at reverseStr11.main(reverseStr11.java:22)
=========================================
I tried to copy the output result but didn't succeeded so I typed it
in.
But as you can see. The piet2.txt file is more succesfull than the
piet.txt file. My question is why?
Perhaps you got another result.
Thanks.
> carla...@hotmail.com (Carla Lamp) wrote in message
> news:<5ddb8ee5.04031...@posting.google.com>...
> for ( String line = in.readLine() ; line != null ; line =
> in.readLine()) {
> StringBuffer strTemp = new StringBuffer(in.readLine());
> String newString = strTemp.reverse().toString();
> System.out.println(newString);
Your progam only works if you have an even number of lines. The problem
is sene in the lines above. In the for loop you read a line (in.readline)
and assign it to line. After that you create a StringBuffer by readin the
next line. Here you should use the original line as:
StringBuffer strTemp = new StringBuffer(line);
//Roger Lindsjö
Thank you all for helping me to find a solution for my problem.
Piet