Java problem (filling an array with the ascii table)

By MrGaribaldi · 6 replies
May 10, 2004
  1. Hello :)

    I've stumbled onto a problem on my latest assignment at the university, and hopefully someone can help me out...

    The assignment says to create an array with 256 slots, which shall be filled with lists. The first node of each list shall be a char from the ascii table .

    Now I know the first 32 entries on the ascii table are non-printing characters, and thus could be skipped, but the assignment calls for an array with 256 slots, so I want to fill them.

    Now what my problem is:

    How can I convert an int to a String with the corresponding ascii character?
    Ie. "101" to "e", "173" to "¡".

    I know that using the following code will convert from characters to the corresponding ascii number, but I can not fathom how to convert it back
            test  = Integer.getInteger(readChar);
            test = new Integer(readChar);
    //can use either of the methods above
            value = test.intValue();
    (Also any streamlining of the above code will also be welcomed :))

    My plan is to use the integer value in the array (due to the first 32 characters) and use .charAt(0) (converted to integer using (some of) the above code) to see where in the array to search for the word).

    I know by doing that I don't need to convert back to characters, but I'll need characters for a full printout of the array so....

    Any help is appreciated!
    TIA :)
  2. Nodsu

    Nodsu TS Rookie Posts: 5,837   +6

    You want to print out a number in human readable form?

    The String class contains static functions to convert numbers.

    You would do sth like:

    I am not responsible for syntax errors. It's not like I write code every day..

    Or maybe I understood your question wrong..
  3. Nic

    Nic TechSpot Paladin Posts: 1,549

    Try this ...

    char c = Character.forDigit (myInt, 10);
    // base 10 used for radix value
  4. MrGaribaldi

    MrGaribaldi TechSpot Ambassador Topic Starter Posts: 2,512

    Thanks for your input people :)

    Alas, I could get neither of your suggestions to work.
    But searching for what a "radix value" is, I came across the answer I was looking for...

    all I had to do was this
    int number = 101;
    char c = (char) number;
    Alas, doing that work, so now I have no more bad excuses to avoid working on the assignment...
    (Been in a foul mood today, only playing Bf1942)
  5. Nodsu

    Nodsu TS Rookie Posts: 5,837   +6

    I misunderstood your problem then..
    You might as well do System.out.print((char)101); then.
  6. MrGaribaldi

    MrGaribaldi TechSpot Ambassador Topic Starter Posts: 2,512

    Yeah... that might've worked too...

    here's (part of) what the final result became...
                firste = newWord.charAt(0);
                place = (int) first;
                if(wordList[place] == null){
                    wordList[place] = new Word(newWord, spam, null);
                }else {
                    next = wordList[place];
  7. emna

    emna TS Rookie

    hi every body!
    perhaps this code will be helpfull for you! i found it here:

    import java.nio.*;
    import java.nio.charset.*;
    import java.util.Arrays;

    public class Convert {
    public static void main(String args[]) {
    Charset asciiCharset = Charset.forName("US-ASCII");
    CharsetDecoder decoder = asciiCharset.newDecoder();
    byte help[] = {72, 101, 108, 112};
    ByteBuffer asciiBytes = ByteBuffer.wrap(help);
    CharBuffer helpChars = null;
    try {
    helpChars = decoder.decode(asciiBytes);
    } catch (CharacterCodingException e) {
    System.err.println("Error decoding");

    bye :knock:
Topic Status:
Not open for further replies.

Similar Topics

Add your comment to this article

You need to be a member to leave a comment. Join thousands of tech enthusiasts and participate.
TechSpot Account You may also...