-
Generate Special character
Hi
can anybody tell me how to generate all the special characters randomly.
-
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
-
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() );
}
}
}
-
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 }$
-
 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.
-
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
-
 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
-
By Murray Foxcroft Z.A. in forum XML
Replies: 3
Last Post: 06-12-2007, 06:45 PM
-
By Russell Jones in forum ASP.NET
Replies: 0
Last Post: 02-28-2003, 11:10 AM
-
By robbie in forum ASP.NET
Replies: 0
Last Post: 02-26-2003, 09:30 PM
-
Replies: 0
Last Post: 10-18-2001, 07:21 PM
-
Replies: 2
Last Post: 06-28-2001, 11:35 PM
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks