Generate Special character


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 7 of 7

Thread: Generate Special character

  1. #1
    Join Date
    Sep 2005
    Posts
    22

    Generate Special character

    Hi

    can anybody tell me how to generate all the special characters randomly.

  2. #2
    Join Date
    Oct 2005
    Posts
    22
    Try to randomly get the numbers and print their ascii characters. It is very easy to do.

    There is a method called random in Math. java.Math.random(). See javadocs.

    Thanks
    G.Rajasekhar

  3. #3
    Join Date
    Oct 2005
    Posts
    40

    Arrow use this

    Use the following program

    Sample output:


    φ
    ?

    +


    (


    Code:
    import java.util.Random;
    /**
     * Mohit Aggarwal
     * aggarwal dot mohit at gmail dot com
     */
    public class SpecialChars
    {
    	static int start[] = {
    		0, 91, 123
    	};
    	static int end[] = {
    		64, 96, 255
    	};
    	
    	static String special = null;
    	
    	static Random random = new Random();
    	
    	public SpecialChars()
    	{
    		if( special == null )
    		{
    			init();
    		}
    	}
    	
    	private void init()
    	{
    		special = "";
    		for( int count = 0; count < start.length; count++ )
    		{
    			int _start = start[ count ];
    			int _end = end[ count ];
    			
    			for( int loop = _start; loop <= _end; loop++ )
    			{
    				special += (char)loop;
    			}
    		}
    	}
    	
    	private char getRandom()
    	{
    		int rand = Math.abs(random.nextInt() % special.length() );
    		return special.charAt( rand );
    	}
    	
    	public static void main( String s[] )
    	{
    		SpecialChars sp = new SpecialChars();
    		
    		for( int count = 0; count < 10; count++ )
    		{
    			System.out.println( sp.getRandom() );
    		}
    	}
    }

  4. #4
    Join Date
    Oct 2005
    Posts
    22
    sorry, it is very complex code for small thing .Try the below code. It will definetely work. I have not used random. But you have character only from 0 to 255 and so try to generate numbers between it and print the character.

    1 import java.io.*;$
    2 class Ascii {$
    3 public static void main(String args[]) {$
    4 System.out.println("Hai");$
    5 for (int i=0;i<255;i++) {$
    6 System.out.print((char)i);$
    7 }$
    8 }$
    9 }$

  5. #5
    Join Date
    Oct 2005
    Posts
    40

    Arrow

    Quote Originally Posted by gulapala
    sorry, it is very complex code for small thing .Try the below code. It will definetely work. I have not used random. But you have character only from 0 to 255 and so try to generate numbers between it and print the character.

    1 import java.io.*;$
    2 class Ascii {$
    3 public static void main(String args[]) {$
    4 System.out.println("Hai");$
    5 for (int i=0;i<255;i++) {$
    6 System.out.print((char)i);$
    7 }$
    8 }$
    9 }$
    If a code is longer in lines of code, it doesn't mean, it is complex. Daina wants to print special characters randomly. Above code, which simply prints all the first 255 chars will also print alphabets.

  6. #6
    Join Date
    Oct 2005
    Posts
    22
    Sorry mohit, If i have hurt you feelings. I don't think daina knows java very well or else she would not have asked simple question. So you should have given the program in a more simple way so that she can understand it. When I meant complex, I mean for a person who is new will be afraid of it, But I accept it is the correct method of doing.

    Once again I apologise, If i have hurt you

  7. #7
    Join Date
    Oct 2005
    Posts
    40

    Question

    Quote Originally Posted by gulapala
    Sorry mohit, If i have hurt you feelings. I don't think daina knows java very well or else she would not have asked simple question. So you should have given the program in a more simple way so that she can understand it. When I meant complex, I mean for a person who is new will be afraid of it, But I accept it is the correct method of doing.

    Once again I apologise, If i have hurt you
    ???

Similar Threads

  1. XMLHTTP special characters
    By Murray Foxcroft Z.A. in forum XML
    Replies: 3
    Last Post: 06-12-2007, 07:45 PM
  2. Replies: 0
    Last Post: 02-28-2003, 12:10 PM
  3. Replies: 0
    Last Post: 02-26-2003, 10:30 PM
  4. XML - Special character handling
    By Pari in forum XML
    Replies: 0
    Last Post: 10-18-2001, 08:21 PM
  5. Replies: 2
    Last Post: 06-29-2001, 12:35 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center