Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

How to enter whole row of elements into 2d array

$
0
0
I'm experimenting with arrays and while I can figure out how to enter the elements of a 2d-array based on user input I cannot figure out how to prompt the user to separately enter entire rows of the 2d-array (with a space in between each number) rather then entering each number then pressing enter. Could I get some help here? I know this will be on my next test, thanks guys!

Here's my code:

import java.util.Scanner;

public class NewArray
{
    public static void main(String[] args)
    {
        Scanner input = new Scanner(System.in);
        System.out.print("Enter the number of rows: ");
        int Rows = input.nextInt();
        System.out.print("Enter the number of columns: ");
        int Columns = input.nextInt();

        int[][] array = new int[Rows][Columns];

        System.out.println("Enter the numbers in array: ");
        for (int i = 0; i < array.length; i++) 
        {
            for (int j = 0; j < array[i].length; j++)
                array[i][j] = input.nextInt();
        }
        System.out.println(isConsecutiveFour(array));
    }

Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>