Advertisement
hemlet

charTest

Feb 23rd, 2014
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.66 KB | None | 0 0
  1. /*
  2.    
  3.     modemnoise.blogspot.com
  4.     Simple program to print out the Printable characters in ASCII.
  5.  
  6.     Print int value, character and hex value
  7.     Test if Java identifier and legal start of
  8.     java identifier
  9.  
  10. */
  11. class chartest {
  12.        
  13.     public static void main(String[] args) {
  14.        
  15.         System.out.println();
  16.         System.out.println("Int\tChar\tHex\tJava\tJavStart");
  17.         System.out.println("----------------------------------------");
  18.        
  19.         // Loop through the printable charaters of the ASCII table
  20.         // Printable character go from 33 == '!' to 126 == '~'
  21.         for (int i = 33; i < 127; i++){
  22.            
  23.             // Example of some formatting argumentss and flags
  24.             // 1$ references 1st attribute 2$ the second etc
  25.             // 04d formats an integral value 4 chars wide with 0s padding to left
  26.             // 04X formats a hex value in uppercase 4 chars wide with 0s padding to left
  27.             // 04x would do the same as above but with lowercase hex
  28.             // Note the 0x in the example below is printed data not reference to attribute
  29.             System.out.printf("%1$04d\t%1$c\t0x%1$04X\t", i);
  30.            
  31.             // Outer if using the Character wrapper class to test
  32.             // if character can be used in a Java identifier
  33.             if (Character.isJavaIdentifierPart(i)){
  34.                
  35.                 System.out.print("y\t");
  36.                
  37.                 // Nested if using the Character wrapper class to test
  38.                 // if character can be used to start a Java identifier
  39.                 // Nested as no point testing if not a legal character
  40.                 // for Java identifier.
  41.                 if (Character.isJavaIdentifierStart(i)){
  42.                     System.out.print("y");
  43.                 } //inner if
  44.                
  45.             } //outer if
  46.            
  47.             // new line after each iteratiion of loop
  48.             System.out.println();          
  49.         }//for
  50.        
  51.     }   //main
  52. }//class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement