Results 1 to 6 of 6

Thread: 3 by 3 array

  1. #1
    fromme87 is offline Member
    Join Date
    Oct 2008
    Posts
    2
    Rep Power
    0

    Default 3 by 3 array

    Hi, it has been awhile since programming in java and now i need it again. I am trying to load and print a 3 by 3 array to start, then add more logic later. I just cant seem to get it correct to load and print. I attatched the file.
    Thanks
    Attached Files Attached Files

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Your code:
    Java Code:
    import java.io.*;
    
    public class Puzzle
    {
    
      public static void main(String[] args) throws IOException
      {
        InputStreamReader reader = new InputStreamReader(System.in);
        BufferedReader keyInput = new BufferedReader(reader);
    
        Integer[][] current = new Integer[3][3];
        Integer[][] ending = new Integer[3][3];
    
        int size = 0;
    
        System.out.println("Type in the integers from top to bottom of each"
          + " column from left to right for a 3X3 with a 0 for the blank.");
        System.out.println(" Press enter after each integer entered.");
    
        String line = null;
    
        for (int i = 0; i < 3; i++)
        {
          for (int j = 0; j < 3; j++)
          {
            while (size != 9)  // Why this while statement??????
            {
              line = keyInput.readLine();
              current[i][j] = new Integer(line);
              size++;
            }
          }
        }
    
        for (int k = 0; k < 3; k++)
        {
          for (int l = 0; l < 3; l++)
            System.out.print(current[l][l]); // don't forget "k" here!
        }
      }
    }
    Please see my comments, especially the one about the while(...) statement. Think about it, and how it can introduce a very bad bug in your program. Come back if you are confused or if this helps.

  3. #3
    fromme87 is offline Member
    Join Date
    Oct 2008
    Posts
    2
    Rep Power
    0

    Default

    well thanks that got me straightened out for now.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    cool. glad it helped.

  5. #5
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    If you have solve the problem please mark the thread as solved. It's really helpful to others.

  6. #6
    Supamagier is offline Senior Member
    Join Date
    Aug 2008
    Posts
    384
    Rep Power
    7

    Default

    Try not to confuse while with if, it's a common made mistake with beginning programmers.
    I die a little on the inside...
    Every time I get shot.

Similar Threads

  1. Array help please
    By buzzdsm in forum New To Java
    Replies: 3
    Last Post: 06-18-2008, 11:43 PM
  2. help with an array.
    By f_the_cook in forum New To Java
    Replies: 7
    Last Post: 06-03-2008, 04:05 AM
  3. Array Reflection: Multi Array Reflection
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-23-2008, 08:08 PM
  4. Array Help
    By bluegreen7hi in forum New To Java
    Replies: 2
    Last Post: 03-28-2008, 02:25 AM
  5. Would appreciate your help with 2d Array..
    By cloudkicker in forum New To Java
    Replies: 1
    Last Post: 02-11-2008, 02:34 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
  •