LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 03-05-2004, 09:22 PM   #1
Armand
Member
 
Registered: Jun 2003
Location: albuquerque, new mexico
Distribution: Windows XP, Debian Linux
Posts: 34

Rep: Reputation: 15
printing unicode characters in JAVA


I'm trying to write a program in JAVA that prints out all of the unicode characters from 32 to 126. What method can I use to print out unicode charatcter x. I'm using a for loop to go from 32 to 126. (I don't want to manually tell the program that 32 is a space, 33 is '!' etc) I just want it to print them out for whatever unicode value I tell it.
I'm a noob in JAVA, this is for my intro to programming class and our teacher sux, she doesn't help much.
 
Old 03-05-2004, 10:08 PM   #2
Armand
Member
 
Registered: Jun 2003
Location: albuquerque, new mexico
Distribution: Windows XP, Debian Linux
Posts: 34

Original Poster
Rep: Reputation: 15
nevermind, I found a post that showed how to print unicode chars. Thanks mtsinc. I had to mess with his example because I need to loop through, and I couldn't do that with a byte variable. So, I just made a counter int variable to count through alongside the byte variable.


Here's the URL for that post: http://www.linuxquestions.org/questi...t=java+unicode

Last edited by Armand; 03-06-2004 at 01:03 PM.
 
Old 03-06-2004, 04:08 AM   #3
german
Member
 
Registered: Jul 2003
Location: Toronto, Canada
Distribution: Debian etch, Gentoo
Posts: 312

Rep: Reputation: 30
could you reply with the URL of the post you speak of? This is a collaborative repository for information we all find.

Thanks

B.
 
Old 03-06-2004, 07:02 AM   #4
Looking_Lost
Senior Member
 
Registered: Apr 2003
Location: Eire
Distribution: Slackware 12.0, OpenSuse 10.3
Posts: 1,120

Rep: Reputation: 45
Like this simply prints out to UTF8 no matter what

Code:
try
  {
   OutputStreamWriter outStream=new OutputStreamWriter(System.out,"UTF8");
   
   Writer out=outStream;
   
   out.write("Hello There\n" );
   out.flush();

   out.write("Character Coding of the output Stream is " + outStream.getEncoding()+"\n");
   out.flush();
   
   out.close();

   }catch(Exception e) {System.err.println(e);}
 
Old 03-06-2004, 01:24 PM   #5
Armand
Member
 
Registered: Jun 2003
Location: albuquerque, new mexico
Distribution: Windows XP, Debian Linux
Posts: 34

Original Poster
Rep: Reputation: 15
Well I am finished with the program, and it looks like this:

** This program is designed to output HTML. If you use it with the normal java command, it will just print html code. The proper way to run it after compiling is:
java AsciiTable >& ascii.html
and the file ascii.html will be a page that displays a table of the ASCII characters.**

Code:
/**
 * Author: Armando ******
 * TA: *********
 * cs152**
 * Description: This program creates html script that displays
 * all of the ASCII printable characters in a table.
 * use java AsciiTable >& ascii.html to generate the HTML doc.
 */
import java.util.Date;

public class AsciiTable
{
    public static void main (String args[])
    {
        // declare variables
        int x;  // used in for loop
        int currentRow;  // what row
        int currentCol;  // what column
        byte asciiChar=32;  // this is for printing out unicode
        int count = 32;  // count variable is to tell when done
        final int NUM_CHARS=94;  // how many characters?
        final int NUM_COL=10;  // how many columns?
        boolean doPrinting=true;  // print printable symbols or not
        byte[] asciibyte = new byte[1];  // for printing the ASCII
        
        try{        
        
        Date today=new Date();  // get the date
        
        // Start printing out some HTML
        System.out.println("<head>\n"
                          +"<title>\n"
                          +"AsciiTable.java - cs152 Armando ******\n"
                          +"</title>\n"
                          +"<body>\n\n"
                          +"<h2>ASCII Character Set</h2>\n"
                          +"<h3>Armando *****, CS152</h3>\n"
                          +today+"\n\n"
                          +"<table cellpadding=2 border=2 bordercolorlight=#00FF00"
                          +" bordercolordark=#FFFF00 bgcolor=#FFFFCC cellspacing=4>\n");
        
        currentRow=NUM_CHARS/NUM_COL;  // calculate rows
        
        while (count<127){  // don't let it count too far
        
        System.out.println("\n<tr>");  // start the row in HTML
        
        for (x=1; x<=NUM_COL;x++){  // start the loop for printing the row
            if (count>=127) {  // tell when to stop printing ASCII
                doPrinting=false;
            }
            
            if (doPrinting==true){  
                
            // put the byte value of the ascii into a variable  
            asciibyte[0]=asciiChar; 
            
            // put the ASCII string from bytevalue into string
            String asciistring = new String(asciibyte,"utf-8");
            
            // print it for the HTML
            System.out.print("<td>"+count+": "+asciistring+"</td>");            
        }
        
            // just print spaces for non-printable HTML
            else {
            System.out.print("<td>"+count+": &nbsp;</td>");
            }
            
            // go to next ascii and count
            asciiChar++;
            count++;
                        
        }
        
        System.out.print("\n</tr>"); // end the row
        
        // go to next row
        currentRow++;
        System.out.println();                            
        }   
        
        // close out the HTML document
        System.out.println("</table></body></html>");
    }
    
    // catch the exceptions
    catch (Exception e){
        System.out.println("Uh oh, "+e);
    }
}
    }
Thanks everyone who replied to this post, and people on that other post that I link to!

**If anyone knows a better way of doing this (mostly printing the ASCII characters) I would be interested in knowing.**

Last edited by Armand; 03-06-2004 at 01:31 PM.
 
Old 03-06-2004, 02:54 PM   #6
Looking_Lost
Senior Member
 
Registered: Apr 2003
Location: Eire
Distribution: Slackware 12.0, OpenSuse 10.3
Posts: 1,120

Rep: Reputation: 45
Not really sure what you're trying to do

if asciiChar is an int then

System.out.println(asciiChar +": "+ char(asciiChar));

should print out the character
 
Old 03-06-2004, 05:42 PM   #7
Armand
Member
 
Registered: Jun 2003
Location: albuquerque, new mexico
Distribution: Windows XP, Debian Linux
Posts: 34

Original Poster
Rep: Reputation: 15
that one gives ".class expected" If I could get that to work, the code would be better though.
 
Old 03-06-2004, 07:47 PM   #8
Looking_Lost
Senior Member
 
Registered: Apr 2003
Location: Eire
Distribution: Slackware 12.0, OpenSuse 10.3
Posts: 1,120

Rep: Reputation: 45
It's just a simple cast from int to char, a char in java is always sixteen bits no matter the platform, this outputs what your doing without the html tags


Code:
public class printchars
{
 

 public static void main(String args[])
 { 
  
   int j=0;
   

   for(int i=32; i<=131;i++)
    {
   
     System.out.print(i + ":\t"  + (char)i +"   ");
  
     
     j++;

    if(j>10)
     {
      System.out.println();
      j=0;
      }
   }
      
 }
}

Last edited by Looking_Lost; 03-06-2004 at 07:50 PM.
 
Old 03-06-2004, 08:51 PM   #9
Armand
Member
 
Registered: Jun 2003
Location: albuquerque, new mexico
Distribution: Windows XP, Debian Linux
Posts: 34

Original Poster
Rep: Reputation: 15
Looks good. Thanks!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Unicode characters in Firefox Ephracis Linux - Software 6 10-14-2005 05:05 PM
Unicode characters looking weird in amaroK Per Linux - Software 0 03-15-2005 03:50 PM
few characters like 'i' not printing somasekhar Linux - Newbie 2 12-17-2004 11:25 AM
Printing japanese characters from Emacs... pAn1k Linux - General 0 09-05-2004 11:16 PM
printing japanese characters... tw001_tw Linux - Software 0 05-02-2004 11:14 PM


All times are GMT -5. The time now is 04:34 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration