Help with Java programming
hi. somebody plz help me with this java programming problem.
i have a string
String m = Nepal is beautiful.
So how do i replace all the characters with '*".
so that the output looks like this.
new string m1 = ***** ** *********
Here it comes...
package replace;
/**
*
*/
import java.io.*;
public class Main {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
String mystring =" ";
System.out.println("Enter the string");
BufferedReader stdin = new BufferedReader( new InputStreamReader( System.in ) );
try
{
mystring = stdin.readLine();
} catch ( IOException e) { };
StringBuffer output =new StringBuffer(' ') ;
for (int i=0;i<mystring.length();i++)
{
if (mystring.charAt(i)!=' ')
output.append("*");
else
output.append(" ");
}
System.out.println(output);
}
}
there are however other ways to do it too...
thanks for the help. but i am supposed to do it without using IO......... Is there a way to do it with only String class?? PLZ help me
Last edited: 20-Feb-08 07:58 AM
Last edited: 20-Feb-08 07:59 AM
In that case, that is much more easier..just give a tweak to the code that i provided...
String m = "Nepal is beautiful";
String m1 = m.replaceAll("\\w", "*");
thanks a lot guys for your time and help...............
Here it goes:
String a="Nepal is beautiful";
String b="";
int n=a.length();
for(int i=0;i<n;i++){
if(Character.isWhitespace(a.charAt(i))){
b+=" ";
}
else{
b+="*";
}
}
System.out.println(b);
Please log in to reply to this post
You can also log in using your Facebook
What people are reading
You might like these other discussions...
· Posts 24
· Viewed 7389
· Likes 2
· Posts 26
· Viewed 2052
· Likes 5
· Posts 13
· Viewed 3432
· Likes 6