Saturday, 15 August 2015

java - How to enter whole row of elements into 2d array -



java - How to enter whole row of elements into 2d array -

i'm experimenting arrays , while can figure out how come in elements of 2d-array based on user input cannot figure out how prompt user separately come in entire rows of 2d-array (with space in between each number) rather entering each number pressing enter. help here? know on next test, guys!

here's code:

import java.util.scanner; public class newarray { public static void main(string[] args) { scanner input = new scanner(system.in); system.out.print("enter number of rows: "); int rows = input.nextint(); system.out.print("enter number of columns: "); int columns = input.nextint(); int[][] array = new int[rows][columns]; system.out.println("enter numbers in array: "); (int = 0; < array.length; i++) { (int j = 0; j < array[i].length; j++) array[i][j] = input.nextint(); } system.out.println(isconsecutivefour(array)); }

you can taking each line input , delimiter take input character character. e.g.

string s = sc.next(); for(int j=0;j<s.length();j++) { array[i][j] = character.getnumericvalue(s.charat(j)); }

or can do

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

java arrays numbers user-input

No comments:

Post a Comment