1

I have a 2D array of 5 rows and 5 columns, all filled with the value 0. How can I make my program do this:

  1. Enter any random combination of row and column like 2-5, without the [] brackets. Just typing 2-5 should be enough to make my program understand I mean row 2 column 5

  2. Assign value I enter to said array in row-column combination.

This is what I got so far. As you can see I have only managed to output the values of all array elements.

import java.util.*;

public class stink {

    public static void main(String[]args){
        int[][] kuk = new int[5][5];
        printMatrix(kuk);
    }

    public static void printMatrix(int[][] matrix)
    {
        for (int row = 0; row < matrix.length; row++)
        {
            for (int col = 0; col < matrix[row].length; col++)
                System.out.printf("%2d", matrix[row][col]);
            System.out.println();
        }
    }
}
3
  • 2
    There is nothing as such 2D array in Java.. It's an Array of Array.. Commented Sep 22, 2012 at 22:39
  • I think the issue you are facing here is to get rows and columns from 2-3 input.. You can use String.split() Commented Sep 22, 2012 at 22:42
  • To start, do you know how to get input from the user? I suggest you first store it as a String. We can proceed from there to turning the input into ints for use with your array. Commented Sep 22, 2012 at 23:00

3 Answers 3

2

you should use Scanner class from java API to get the inputs from the user like in the below code. pass the inputs with a delimiter like if you want to have 2X3 array pass like 2-3 where '-' is a delimiter. here are the links for String the and scanner java API.

        Scanner sc = new Scanner(System.in);
    System.out.println("please enter two numbers");
    String inputs = sc.next();
    int a=Integer.valueOf(inputs.split("-")[0]);
    int b=Integer.valueOf(inputs.split("-")[1]);;
    System.out.println(a + " " + b);
    int[][] x = new int[a][b];
    System.out.println(x.length);
Sign up to request clarification or add additional context in comments.

2 Comments

@chaitanya10... By providing direct answer to the question, you are not helping him learn.. It's always better to provide resource, so that one can see and learn even more than required..
@RohitJain hann,... you are right.tbh i dont myself think this is the optimal solution. this can be done in many other ways...
0

this is not a copy/paste ready answer, but it should at least give you an indication on how you could handle this.

        int rows = 0;
        int columns = 0;
        Scanner scan = new Scanner(System.in);
        System.out.println("Rows: ");
        rows = scan.nextInt();
        System.out.println("Columns: ");
        columns = scan.nextInt();           
        int[][] kuk = new int[rows][columns];

Comments

0
import java.util.*;
public class stink
{
    public static void main(String[]args)
    {
        int[][] kuk = new int[5][5];
        // Where x and y are keys, integer is the integer you want to push
        pushIntoMatrix(kuk, x, y, integer);
        //kuk = pushIntoMatrix(kuk, x, y, integer); // Use if you want the method to return a value.
    }
    public static void pushIntoMatrix(int[][] matrix, int x, int y, int integer)
    //public static int[][] pushIntoMatrix(int[][] matrix, int x, int y, int integer) // Use if you want to return the array.
    {
        matrix[x][y] = integer;
        //return matrix; // Use if you want to return the array.
    }
}

Since as you know, any data type in Java that's not a primitive is a reference, passing the kuk array into the method would affect the actual array reference. You can set a return in pushIntoMatrix() if you want, but you don't have to.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.