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

  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


  2. #2
    Member
    Join Date
    Aug 2013
    Posts
    95
    Thanks
    3
    Thanked 14 Times in 14 Posts

    Default Re: Storing a string in a 2D array

    How about a 2d array of characters. That way you could say image[i][j] = '0';

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

    Default Re: Storing a string in a 2D array

    That also works. Unfortunately, my other problem still exists Thanks though!

  4. #4
    Member
    Join Date
    Aug 2013
    Posts
    95
    Thanks
    3
    Thanked 14 Times in 14 Posts

    Default Re: Storing a string in a 2D array

    What is your other problem? You should be able to accomplish all that you listed with this 2d char array

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

    Default Re: Storing a string in a 2D array

    When I try to call createImage, it returns a number of blank lines (same as the columns I enter) along with its memory location. This is my updated createImage method, by the way.

    public static char[][] createImage(int row, int column)
    {        
        char[][] image = new char[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;
    }

  6. #6
    Member
    Join Date
    Aug 2013
    Posts
    95
    Thanks
    3
    Thanked 14 Times in 14 Posts

    Default Re: Storing a string in a 2D array

    Well, you arent printing anything... you're just printing a new line. you should print out the actual image slot..

      for (int i = 0; i < column; i++)
    		    {
    		        for (int j = 0; j < row; j++)
    		        {
    		            image[j][i] = '0';
    		            System.out.printf("%c ", image[j][i]);
    		        }
    		        System.out.println();
    		    }

    I might be confused on what you actually want the method to achieve.

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

    Default Re: Storing a string in a 2D array

    It worked! Thanks! Before I continue, this assignment is actually a programming challenge from a website. If you want, you can take a look at it here: Programming Challenges-Graphical Editor

    I looked through the requirements once again and the image is only outputted when the user enters the S command. So if I created a 5 by 6 image with the I command, then replaced the 3rd pixel in the 2nd row, I would only output the new image. That's what I'm not sure how to do. Sorry if I'm so confusing!

    Input:

    I 5 6

    L 3 2 F
    Output:

    00000
    00000
    00000
    00000
    00000
    00000

  8. #8
    Member
    Join Date
    Aug 2013
    Posts
    95
    Thanks
    3
    Thanked 14 Times in 14 Posts

    Default Re: Storing a string in a 2D array

    You should make a method that takes those parameters. Then you can go to that row and column and put in the letter you want manually

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

    Default Re: Storing a string in a 2D array

    Whenever I print the image, the output also includes the memory location of the array. How can I get rid of that? I've tried Arrays.toString(), but it ouputs the memory location of each column. I've also tried Arrays.deepToString(), but that outs the image like so:

    [[0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0]]

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