Results 1 to 2 of 2
  1. #1
    Chasingxsuns is offline Member
    Join Date
    Jul 2009
    Posts
    56
    Rep Power
    0

    Default Cast int to char

    Java Code:
        public void updateGrid(ListStack<Position> stack) {
            LinearNode<Position> trav = stack.getTop();
            int count = stack.size();
            //go through stack, apply position numbers to the grid
            while(trav.getNext() != null) {
                char val = (char) (count);
                System.out.println(val);
    
                grid.getMap()[trav.getElement().getX()][trav.getElement().getY()] = val;
                trav = trav.getNext();
                count--;
            }   //end while
        }
    Instead of printing a character value of count, all it prints is a blank line (or maybe ' '). I don't get why. Is that not the correct syntax for casting an int to a char??

  2. #2
    Turtle is offline Member
    Join Date
    Nov 2007
    Location
    New Zealand
    Posts
    36
    Rep Power
    0

    Default

    Your code does this...

    // Get the length of this list and store it in count
    // Convert the number count to a character and store in val
    // Print the character stored in val

    Are you sure this is what you want to accomplish?
    Because lower character values are weird, some are just blanks.

    For example:
    Java Code:
    class Test {
      public static void main(String[] args) {
        for (int i=0; i<92; i++) {
    	    char c = (char) i;
    	    System.out.println( i+ "[" +c+ "]" );
        }
      }
    }
    (I don't see the goal of your code.)

Similar Threads

  1. Cannot cast JNLPAppletLauncher to itself
    By kingofearth in forum Java Applets
    Replies: 0
    Last Post: 01-19-2010, 07:37 PM
  2. Help with cast, equals and clone
    By nellaf in forum New To Java
    Replies: 1
    Last Post: 04-19-2009, 06:57 AM
  3. Cast From Image to String
    By Deepa in forum New To Java
    Replies: 4
    Last Post: 03-03-2009, 02:07 PM
  4. drawing char by char with Graphics
    By diggitydoggz in forum New To Java
    Replies: 5
    Last Post: 12-27-2008, 12:49 PM
  5. How can I cast Object as an int
    By romina in forum New To Java
    Replies: 1
    Last Post: 07-18-2007, 11:20 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •