Results 1 to 5 of 5

Thread: array problem

  1. #1
    amani
    Guest

    array problem

    I have a strig array of nine elements say
    String temp[] = {"1", "2", "3", "4", "5", "6", "7", "8", "0"};

    I want to break this in to a double dimensional array
    like
    int[][] puz = { { 1, 2, 3 }, { 4, 5, 6 }, { 7,8, 0 } };

    I am getting
    please help me

  2. #2
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    So what you actualy want to do is convert the String array that you have into a two dimensional integer array?
    Last edited by Dilenger4; Apr 6th, 2002 at 02:33 PM.

  3. #3
    amani
    Guest
    yes you got it right

  4. #4
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    What i probably would do is loop through the string array that you have and invoke the Integer.parseInt(String s) method.

  5. #5
    Hyperactive Member CaptainPinko's Avatar
    Join Date
    Jan 2001
    Location
    London, Ontario, Canada
    Posts
    332
    how's this?

    Code:
    String temp[] = {"1", "2", "3", "4", "5", "6", "7", "8", "0"}; 
    int [] [] td = new int [3] [3];
    
    for (int i = 0; i < 3; i++)
          for (int j = 0; j , 3; j++)
               td [i] [j] = Integer.parseInt ( temp [j+(i*3)]);
    that should do it, though there might be an error inside those for loops... oh well =D!
    "There are only two things that are infinite. The universe and human stupidity... and the universe I'm not sure about." - Einstein

    If you are programming in Java use www.NetBeans.org

Posting Permissions

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



Click Here to Expand Forum to Full Width