Results 1 to 2 of 2
  1. #1
    ukemasta is offline Member
    Join Date
    Jan 2010
    Posts
    4
    Rep Power
    0

    Default simple ASCII Table help

    Hi i need to create a table that looks like this



    I also need to set from integer value 32 to 126.

    Does anyone know how to get the character column on that picture

    this is what code I have so far. Any help would be greatly appreciated

    Thank you very much


    Java Code:
    /**
     * Write a description of class hw2 here.
     * 
     * @author (your name) 
     * @version (a version number or a date)
     */
    public class hw2
    {
        // instance variables - replace the example below with your own
        private int x;
    
        /**
         * Constructor for objects of class hw2
         */
        public hw2()
        {
            // initialise instance variables
            x = 0;
        }
    
        /**
         * An example of a method - replace this comment with your own
         * 
         * @param  y   a sample parameter for a method
         * @return     the sum of x and y 
         */
        public void one()
        {
            // put your code here
            System.out.println("Value   Character");
            for(int i = 32; i < 127; i++){
                System.out.println( i );
            }
        }
    }

  2. #2
    travishein's Avatar
    travishein is offline Senior Member
    Join Date
    Sep 2009
    Location
    Canada
    Posts
    684
    Rep Power
    6

    Default

    uh, cast the integer i here as a char
    such as
    Java Code:
    System.out.println( (char) i);
    or to put it self contained examply,
    Java Code:
    /**
     * @author thein
     *
     */
    public class AciiTable {
      public static void main(String[] args) {
        System.out.println("Value  Hex     Character");
        for(int i = 32; i < 127; i++){
          // because the i will go from 2 decimals to 3 decimal places
          // we convert it to a string here before just printing it, 
          // so that we can enforce its length always 3 for pretty printing.
          String value = String.valueOf(i);
          if (value.length() < 3) {
            value = " " + value;
          }
          String hex = Integer.toHexString(i);
          System.out.println( value + "    " + hex + "      " + (char)i);
        }
      }
    }
    or more eloquently,
    Java Code:
    /**
     * @author thein
     *
     */
    public class TwoColumnAciiTable {
    
      private static String render(int i) {
        // because the i will go from 2 decimals to 3 decimal places
        // we convert it to a string here before just printing it, 
        // so that we can enforce its length always 3 for pretty printing.
        String value = String.valueOf(i);
        if (value.length() < 3) {
          value = " " + value;
        }
        String hex = Integer.toHexString(i);
        return new String(value + "    " + hex + "      " + (char)i);
      }
    
      public static void main(String[] args) {
        System.out.println("Value  Hex     Character     Value  Hex     Character");
        int start = 32;
        int end = 127;
        int mid = (end-start)/2 + 1;
        for(int i = 0; i < mid; i++) {
          String line = render(i+start) 
          + "             ";
          if (i + start + mid < end) {
            line += render(i + start + mid);
          }
          System.out.println(line);
        }
      }
    }
    Last edited by travishein; 01-20-2010 at 10:38 PM.

Similar Threads

  1. Replies: 1
    Last Post: 01-08-2010, 07:19 AM
  2. Super simple table applet
    By danwoods in forum New To Java
    Replies: 4
    Last Post: 10-21-2009, 01:58 PM
  3. Getting ascii from a String
    By ali_sakar in forum New To Java
    Replies: 1
    Last Post: 03-07-2009, 11:11 AM
  4. Simple Table Example
    By Java Tip in forum SWT Tips
    Replies: 0
    Last Post: 07-07-2008, 05:58 PM
  5. Displaying ASCII Codes in a table
    By Java Tip in forum SWT Tips
    Replies: 0
    Last Post: 07-07-2008, 05:56 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
  •