Help with Java programming - Sajha Mobile
SAJHA MOBILE
Help with Java programming
Posts 8 · Viewed 4136 · Go to Last Post
dhimaar1
· Snapshot
Like · Likedby · 0

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 = ***** ** *********

 

sangfroid
· Snapshot
Like · Liked by · 0
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);
    }

}

sangfroid
· Snapshot
Like · Liked by · 0
there are however other ways to do it too...
dhimaar1
· Snapshot
Like · Liked by · 0
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
sangfroid
· Snapshot
Like · Liked by · 0
In that case, that is much more easier..just give a tweak to the code that i provided...
Echoes
· Snapshot
Like · Liked by · 0

String m = "Nepal is beautiful";

String m1 = m.replaceAll("\\w", "*");

dhimaar1
· Snapshot
Like · Liked by · 0

thanks a lot guys for your time and help...............

LondonBridge
· Snapshot
Like · Liked by · 0
      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
View in Desktop
What people are reading
You might like these other discussions...
· Posts 2 · Viewed 215
· Posts 1 · Viewed 122
· Posts 24 · Viewed 7389 · Likes 2
· Posts 26 · Viewed 2052 · Likes 5
· Posts 1 · Viewed 72
· Posts 1 · Viewed 82
· Posts 1 · Viewed 148
· Posts 2 · Viewed 251
· Posts 13 · Viewed 3432 · Likes 6
· Posts 8 · Viewed 2577



Your Banner Here
Travel Partners
Travel House Nepal