Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 9 of 9

Thread: Storing a string in a 2D array

Threaded View

  1. #1
    Junior Member
    Join Date
    Sep 2014
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Storing a string in a 2D array

    Hi!

    Edit: Link to my assignment: Programming Challenges-Graphical Editor

    One of the requirements of my programming assignment is that a user enters a command along with two numbers. For example, if I entered the following command:

    I 5 6
    The program create an image like this (it wouldn't be outputted though):

    00000
    00000
    00000
    00000
    00000
    00000
    It creates a 5 x 6 "image". This is where my troubles begin. The program should also accepts other commands, such as:

    L 3 2 F

    which would produce (also not outputted):

    00000
    00F00
    00000
    00000
    00000
    00000
    Here is my method for creating an "image" with an M * N array

    for (int i = 0; i < column; i++)
    {
        for (int j = 0; j < row; j++)
        {
            System.out.print("0");
        }
        System.out.println();
    }

    The code works, but I need to store the image in an array so it can be changed by other methods (I can't create the image manually every time). I tried doing something like this but it doesn't work:

    public static String[][] createImage(int row, int column)
    {        
        String[][] image = new String[row][column];
        for (int i = 0; i < column; i++)
        {
            for (int j = 0; j < row; j++)
            {
                image[j][i] = "0";
            } 
            System.out.println();
        }
        return image;
    }

    This method outputs as many blank lines as the columns I entered and the memory location of image.

    So my question is: how would I store "0" in a 2D array so that it can be accessed and changed by other methods?
    Or, am I using a 2D array incorrectly and will the image have to be created manually every time? If so, how would I output image if it is created in a separate method?

    Thank you!
    Last edited by namesnipe; September 24th, 2014 at 11:15 PM. Reason: Added link to assignment


Similar Threads

  1. Storing random numbers into a String field
    By ajw1993 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 23rd, 2013, 10:44 AM
  2. Replies: 1
    Last Post: March 15th, 2013, 03:04 AM
  3. Help string not storing in Variable
    By nava111 in forum What's Wrong With My Code?
    Replies: 7
    Last Post: February 27th, 2013, 11:32 AM
  4. advice needed on storing string into arraylist and printing it out.
    By jong2009 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: October 23rd, 2012, 10:16 AM
  5. [SOLVED] 2 dimensional array storing help!
    By jts0541 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 31st, 2012, 03:31 PM