Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / March 2004

Tip: Looking for answers? Try searching our database.

reverseing strings in file

Thread view: 
piet - 11 Mar 2004 00:26 GMT
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 - 11 Mar 2004 01:44 GMT
> 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
[quoted text clipped - 3 lines]
> 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.

Signature

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 - 11 Mar 2004 04:16 GMT
You need to define main as static.
public static void main(String args[])...
> Hi,
>
[quoted text clipped - 51 lines]
>
> Piet
piet - 18 Mar 2004 02:36 GMT
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.

> You need to define main as static.
> public static void main(String args[])...
[quoted text clipped - 53 lines]
> >
> > Piet
Carla Lamp - 18 Mar 2004 12:30 GMT
> 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
[quoted text clipped - 35 lines]
> > > 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);

}

> > >    }
> > >
[quoted text clipped - 13 lines]
> > >
> > > Piet

Use a for loop as shown above.
piet - 19 Mar 2004 04:30 GMT
> > 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
[quoted text clipped - 64 lines]
>
> 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.
Roger Lindsjö - 19 Mar 2004 16:53 GMT
>   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 - 22 Mar 2004 00:58 GMT
>  
> >   for ( String line = in.readLine() ; line != null ; line =
[quoted text clipped - 10 lines]
>
> //Roger Lindsjö

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

Piet


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.