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 17 of 17

Thread: How can I add a user input into a 2D- array and print it?

  1. #1
    Junior Member
    Join Date
    Mar 2019
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Question How can I add a user input into a 2D- array and print it?

    //THIS IS THE CODE I HAVE SO FAR:
     
    import java.util.Arrays;
    import java.util.Scanner;
    import java.io.File;
    import java.io.FileWriter;
    import java.awt.event.KeyEvent;
    import javax.swing.JOptionPane;
     
     
    public class GridInput {
            static int X = 6;
            static int Y = 7;
    public static void main(String[]args) {
    String [][] Grid = new String [X][Y];
     
    System.out.println("Hello R, please make your move! Choose a number from 0-6");
    int R = StdIn.readInt();
     
            System.out.print("  ");  
            for (int i = 0; i <= X; i++) {
                    if (i > 0) {           
                        System.out.print(i - 1);
                    }
                    for (int j = 0; j < Y; j++) {
                        if (i == 0)
                            System.out.print(j + " ");
                        else;
                            System.out.print(Grid[i-1][j] = " *");
                    }
            System.out.println();
                }
            }
    }

    //WHAT I WANT TO PRINT LOOKS LIKE THIS (SAY USER INPUT FOR R IS "4"):

    0 1 2 3 4 5 6
    0 * * * * * * * *
    1 * * * * * * * *
    2 * * * * * * * *
    3 * * * * * * * *
    4 * * * * * * * *
    5 * * * * * R * *

    //THEN THERE SHOULD BE ANOTHER USER INPUT WHICH CHANGES THE GRID AGAIN.
    //IF THE SECOND USER INPUT IS 4 AGAIN:

    0 1 2 3 4 5 6
    0 * * * * * * * *
    1 * * * * * * * *
    2 * * * * * * * *
    3 * * * * * * * *
    4 * * * * * R * *
    5 * * * * * R * *

    //I WANT THE GRID TO LOOK EXACTLY LIKE THIS
    Last edited by Norm; March 29th, 2019 at 09:41 AM. Reason: Moved ending code tag to after code

  2. #2
    Junior Member
    Join Date
    Mar 2019
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Question How can I add a user input into a 2D- array and print it?

    //THIS IS THE CODE I HAVE SO FAR:
     
    import java.util.Arrays;
    import java.util.Scanner;
    import java.io.File;
    import java.io.FileWriter;
    import java.awt.event.KeyEvent;
    import javax.swing.JOptionPane;
     
     
    public class GridInput {
            static int X = 6;
            static int Y = 7;
    public static void main(String[]args) {
    String [][] Grid = new String [X][Y];
     
    System.out.println("Hello R, please make your move! Choose a number from 0-6");
    int R = StdIn.readInt();
     
            System.out.print("  ");  
            for (int i = 0; i <= X; i++) {
                    if (i > 0) {           
                        System.out.print(i - 1);
                    }
                    for (int j = 0; j < Y; j++) {
                        if (i == 0)
                            System.out.print(j + " ");
                        else;
                            System.out.print(Grid[i-1][j] = " *");
                    }
            System.out.println();
                }
            }
    }
     
     
    //WHAT I WANT TO PRINT LOOKS LIKE THIS (SAY USER INPUT FOR R IS "4"):
     
      0 1 2 3 4 5 6
    0 * * * * * * * 
    1 * * * * * * * 
    2 * * * * * * *  
    3 * * * * * * * 
    4 * * * * * * *  
    5 * * * * R * *
     
    //THEN THERE SHOULD BE ANOTHER USER INPUT WHICH CHANGES THE GRID AGAIN. 
    //IF THE SECOND USER INPUT IS 4 AGAIN:
     
      0 1 2 3 4 5 6
    0 * * * * * * * 
    1 * * * * * * * 
    2 * * * * * * *  
    3 * * * * * * * 
    4 * * * * R * *  
    5 * * * * R * *
     
    //I WANT THE GRID TO LOOK EXACTLY LIKE THIS

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: How can I add a user input into a 2D- array and print it?

    USER INPUT FOR R IS "4"
    Can you explain how a user input of "4" can be used to place an "R" at a specific row and column in a 2 dim array?

    I WANT THE GRID TO LOOK EXACTLY LIKE THIS
    Please explain how the row and column for placing the "R" is computed?
    If you don't understand my answer, don't ignore it, ask a question.

  4. The Following User Says Thank You to Norm For This Useful Post:

    Fan Zhendong (March 29th, 2019)

  5. #4
    Junior Member
    Join Date
    Mar 2019
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: How can I add a user input into a 2D- array and print it?

    Quote Originally Posted by Norm View Post
    Can you explain how a user input of "4" can be used to place an "R" at a specific row and column in a 2 dim array?
    If I understand your question right... yeah, that was a bit vague of me. It is like the game Connect 4. The user input refers to the Row (horisontal). The user picks the row in which the "R" will "fall" down and the R goes to the bottom of the column. (Sorry, I hope this explanation is ok) So when the user says "4", it refers to the row (row 5, which is numbered as 4) and that is the row in which the R will be placed. The R goes to the bottom column of that row.


    If I understand right, the above also answers your second question.

  6. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: How can I add a user input into a 2D- array and print it?

    The user picks the row
    Did you mean column?

    Ok, I think I understand. What problems are you having placing the "R" in the array?
    If you don't understand my answer, don't ignore it, ask a question.

  7. #6
    Junior Member
    Join Date
    Mar 2019
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: How can I add a user input into a 2D- array and print it?

    Quote Originally Posted by Norm View Post
    Did you mean column?

    Ok, I think I understand. What problems are you having placing the "R" in the array?
    I mean Row (horizontal/ above)

    I don't know how to place "R" into the array at all(or to print the array). I have tried a few things, but nothing worked. I am very new to programming...

  8. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: How can I add a user input into a 2D- array and print it?

    I mean Row (horizontal/ above)
    I don't understand how a user picking a row puts a value in a column. How does the program pick the column?

    The bottom display of a filled in grid in post#2 shows the "R"s in column 4 not in row 4.


    how to place "R" into the array
    Use an assignment statement:
      theArray[row][column] = theValue;
    If you don't understand my answer, don't ignore it, ask a question.

  9. #8
    Junior Member
    Join Date
    Mar 2019
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: How can I add a user input into a 2D- array and print it?

    Say the user input is "4"
    That means they are choosing the row in which the R will be placed. The program should then automatically place R in the bottom column of that row (like you drop a disc down a row in Connect4).

  10. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: How can I add a user input into a 2D- array and print it?

    he program should then automatically place R in the bottom column of that row
    That is confusing.
    For me: Rows are horizontal (left, right) and columns are vertical(up, down).
    There isn't a bottom column of a row. There is a bottom row of a column.
    If you don't understand my answer, don't ignore it, ask a question.

  11. #10
    Junior Member
    Join Date
    Mar 2019
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: How can I add a user input into a 2D- array and print it?

    O ok, I think I understand. Yeah rows (left, right) and columns (up, down).

    Anyway, what I mean should happen is like this for example: (Ignore the awkward spacing - just tried to make it easier to read)

    1. The user's first input is 1, then the following happens and is printed:
       0    1    2   3    4    5    6
    0  *    *    *   *    *    *    * 
    1  *    *    *   *    *    *    * 
    2  *    *    *   *    *    *    *  
    3  *    *    *   *    *    *    *  
    4  *    *    *   *    *    *    *  
    5  *    R    *   *    *    *    *


    2. Then the user inputs 4:
       0    1    2   3    4   5   6
    0  *    *    *   *    *   *   * 
    1  *    *    *   *    *   *   * 
    2  *    *    *   *    *   *   *  
    3  *    *    *   *    *   *   *  
    4  *    *    *   *    *   *   *  
    5  *    R    *   *    R   *   *



    3. If the user then inputs 4 again:
       0    1    2   3   4   5   6
    0  *    *    *   *   *   *   * 
    1  *    *    *   *   *   *   * 
    2  *    *    *   *   *   *   *  
    3  *    *    *   *   *   *   *  
    4  *    *    *   *   R   *   *  
    5  *    R    *   *   R   *   *


    Now, I have no idea how to code for that to happen
    or what this piece of code means:
     theArray[row][column] = theValue;
    Thanks for listening to me try to explain.
    Last edited by Fan Zhendong; March 29th, 2019 at 02:11 PM.

  12. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: How can I add a user input into a 2D- array and print it?

    what this piece of code means:
     theArray[row][column] = theValue;
    theArray - the name of the array
    row - the row index (an int)
    column - the column index (an int)
    = - an assignment statement
    theValue - the value to be assigned to the 2 dim array at location: row, column

    The tutorial: https://docs.oracle.com/javase/tutor...ts/arrays.html

    for that to happen
    Before trying to write any code, work out the logic.
    Can you explain how you would do it manually? Given an input from a user and a grid with the 0,0 point to the top left, what would you need to do to put a "R" on the grid? What simple steps would you take?
    And then later when there are some "R"s already on the grid, what do you need to do to add another "R"?
    If you don't understand my answer, don't ignore it, ask a question.

  13. #12
    Junior Member
    Join Date
    Mar 2019
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: How can I add a user input into a 2D- array and print it?

    Oh ok, I understand.

    But how do I incorporate that in my initial code and print the "changed" array?

    --- Update ---

    First I would add this:

    int R = StdIn.readInt();
    Grid[6][R+1) = " R";


    That would make the specific point in the coordinate in the grid = " R".
    (6 because that's the bottom column and R+1, because I want it in that row)

  14. #13
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: How can I add a user input into a 2D- array and print it?

    print the "changed" array?
    Create a method that prints the array.
    Then call it whenever the array has been changed.
    If you don't understand my answer, don't ignore it, ask a question.

  15. #14
    Junior Member
    Join Date
    Mar 2019
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: How can I add a user input into a 2D- array and print it?

    Ok, I will look into that...

    So I can just create another method any time and use it for something specific (in this case, whenever I want to print the Grid/ array)?

  16. #15
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: How can I add a user input into a 2D- array and print it?

    First I would add this:
    You need to work out the logic before writing code.
    What if there is already an "R" in the bottom row?

    can just create another method any time
    Yes. That is the usual way to write programs - create methods for jobs that are done repeatedly.
    If you don't understand my answer, don't ignore it, ask a question.

  17. #16
    Junior Member
    Join Date
    Mar 2019
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: How can I add a user input into a 2D- array and print it?

    Anyway, I will read up on that - I don't mean to bother.
    Thanks for all your help!!

    --- Update ---

    Then I don't know yet - I will have to figure that out.

    When it something is already in the bottom row, I will have to change the grid accordingly, and probably use an if statement.

    Say, if R = 5:
    Grid[X][R+1] = " R";

    then when the same R is printed later on:
    Grid[X-1][R+1] = " R";

    I will figure that out.

  18. #17
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: How can I add a user input into a 2D- array and print it?

    When it something is already in the bottom row,
    Will the logic start at the bottom and work towards the top
    or will it start at the top and work towards the bottom?

    I will have to change the grid accordingly
    Ok, what are the details for that? The computer does not understand "accordingly", you will have to explicitly work out the details.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Getting user input and storing it into an array
    By J0k3r in forum File I/O & Other I/O Streams
    Replies: 15
    Last Post: March 14th, 2019, 08:09 PM
  2. Replies: 4
    Last Post: April 17th, 2014, 11:49 AM
  3. Trying to validate a character user input in an array ?
    By lizzy2 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 20th, 2013, 03:04 PM
  4. How to add to an array with user input
    By miller4103 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 12th, 2013, 10:27 PM
  5. How to print the day of the week from user input?
    By f0x in forum Java Theory & Questions
    Replies: 2
    Last Post: October 31st, 2011, 05:33 PM

Tags for this Thread