wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Can you complete this code please? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Can you complete this code please?" Watch "Can you complete this code please?" New topic
Author

Can you complete this code please?

Hanadi Al-Naimi
Greenhorn

Joined: Dec 25, 2004
Posts: 6
Hi,

I wish that I find a solution for this problem..
I asked to write a code that replacing each 'a' character on a string with a '?' character..

So I wrote this code but I could'nt continue it

import java.io.*;
public class Example4 {

public static void main(String[] args)throws IOException
{

DataInputStream Br = new DataInputStream(System.in);
String Word= new String();

System.out.print("Enter any word please>>");
Word= Br.readLine().trim();

String NewWord = Replace(Word);
System.out.println("\n The word was: "+Word+"\n The word after: "+NewWord);


}
public static String Replace ( String W )
{
String New= new String();
for ( int i=0 ; i < W.length() ; i++ )
if ( W.charAt(i)== 'A' || W.charAt(i)== 'a' )
// System.out.print(W.charAt(i));
// New=New.concat("?");


return New;

}
}
Joyce Lee
Ranch Hand

Joined: Jul 11, 2003
Posts: 1392
Hi Hanadi,

Why not use String's replace() method?



Alternatively, replace A or a with ? using regular expression.



Check out the String API for more details.

Joyce
[ April 11, 2005: Message edited by: Joyce Lee ]
Anselm Paulinus
Ranch Hand

Joined: Sep 05, 2003
Posts: 389
Joyce Lee gave a perfect solution to your problem; but if you insist in doing it the complex way as you are currently doing then you going to define new as StringBuffer and change the line New=New.concat("?"); with StringBuffer append() method.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Can you complete this code please?