Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

reverseing strings in file

0 views
Skip to first unread message

piet

unread,
Mar 10, 2004, 7:26:26 PM3/10/04
to
Hi,

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

Andrew Thompson

unread,
Mar 10, 2004, 8:44:33 PM3/10/04
to
On 10 Mar 2004 16:26:26 -0800, piet wrote:

> 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

unread,
Mar 10, 2004, 11:16:43 PM3/10/04
to
You need to define main as static.
public static void main(String args[])...
"piet" <pwv...@12move.nl> wrote in message
news:c43b6d0d.04031...@posting.google.com...

piet

unread,
Mar 17, 2004, 8:36:09 PM3/17/04
to
Thank you winbatch. Sorry for my late respons. The result is see is
now that I can only read one line of the text file. It is a step
forward for me. The second step is to find out how to read the whole
textfile based on this piece of code.

"Winbatch" <winb...@techie.com> wrote in message news:<LKR3c.15570$c73.4...@twister.nyc.rr.com>...

Carla Lamp

unread,
Mar 18, 2004, 6:30:39 AM3/18/04
to
pwv...@12move.nl (piet) wrote in message news:<c43b6d0d.0403...@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);
> > >
}

> > > }
> > >
> > > 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.

piet

unread,
Mar 18, 2004, 10:30:32 PM3/18/04
to
carla...@hotmail.com (Carla Lamp) wrote in message news:<5ddb8ee5.04031...@posting.google.com>...


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.

Roger Lindsjö

unread,
Mar 19, 2004, 10:53:49 AM3/19/04
to
On Fri, 19 Mar 2004 04:30:32 +0100, piet wrote:

> 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ö

piet

unread,
Mar 21, 2004, 6:58:50 PM3/21/04
to
Roger Lindsjö <ro...@tilialacus.net> wrote in message news:<pan.2004.03.19.16....@tilialacus.net>...


Thank you all for helping me to find a solution for my problem.

Piet

0 new messages