File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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

Can you complete this code please?

Hanadi Al-Naimi

Joined: Dec 25, 2004
Posts: 6

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

public class Example4 {

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

DataInputStream Br = new DataInputStream(;
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.

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

Joined: Sep 05, 2003
Posts: 390
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:
subject: Can you complete this code please?
It's not a secret anymore!