user1905170
user1905170

Reputation: 67

How to enter whole row of elements into 2d array

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));
    }

Upvotes: 0

Views: 9199

Answers (4)

Mosbius8
Mosbius8

Reputation: 119

You can also do it by taking the each line as an input and then with delimiter take the input character by character. e.g.

String s = sc.next();

        for(int j=0;j<s.length();j++)
        {
          array[i][j] = Character.getNumericValue(s.charAt(j));

        }

or you can do

array[i][j] = s.charAt(j) - '0';

Upvotes: 1

Lee Meador
Lee Meador

Reputation: 12985

One way to do what you ask is to input what they type as an entire line. Then you look at the line and check for enough values and fail in some fashion if they don't enter it the way you wnat.

The problem is that that makes it really irritating for the user. What if it needs 10 numbers and they enter 9 or 11 because of a miscount or an extra space where a period should be? What are you going to do?

A lot depends on how your program responds to the user not doing it "right."

So if they enter too many, you can echo the first numbers (up to the required number) and ask if its ok to just use those. They then answer Y or N.

If they enter too few, you could echo the ones they entered and then ask for some more numbers to fill out the required count.

All this turns into a lot of code to write and it can be persnickety--you have to get it just right or, again, it confused the user.

Another possible solution is to let them enter them one by one and you echo information to them so they know where they are. Something like "Please enter element 4 on row 3: " would help them keep track of it.

Perhaps the more you look into this, the more you can see the value of today's common user interfaces whether for standalone apps or web applications. On the web, for example, you can show them a form with rows and columns of text boxes and easily redisplay that page if some of the boxes aren't filled in right. The console based user interface was used many years ago but it leaves much to be desired.

Upvotes: 0

Mr. Polywhirl
Mr. Polywhirl

Reputation: 48600

Well, you could prompt them for every row...

for (int i = 0; i < array.length; i++) {
  // Prompt user to enter column values for each row
  System.out.printf("Enter %d integers for row %d/%d (delimeted by spaces): ",
    columns, i+1, rows);
  for (int j = 0; j < array[i].length; j++)
    array[i][j] = input.nextInt();
}

output

user@ubuntu:~$ java NewArray 
Enter the number of rows: 4
Enter the number of columns: 3
Enter the numbers in array: 
Enter 3 integers for row 1/4 (delimeted by spaces): 1 2 3
Enter 3 integers for row 2/4 (delimeted by spaces): 4 5 6
Enter 3 integers for row 3/4 (delimeted by spaces): 7 8 9
Enter 3 integers for row 4/4 (delimeted by spaces): 10 11 12

Upvotes: 0

Kent
Kent

Reputation: 195039

loop rows times, and String l = input.nextLine();

split the string with delimiter (space or comma or semicolon you can define it).

of course, implement validation logic. e.g Nan case, splitted array size doesn't match columns case etc.

btw, based on java naming convention, better using rows, columns as name. otherwise they look like classes....

Upvotes: 0

Related Questions