How can i insert multiple input in one line in 2dArray looping
So i want my output to be something like this:
N = 3
userinput1 | userinput2 | userinput3
userinput4 | userinput5 | userinput6
userinput7 | userinput8 | userinput9
but the problem is after i insert my userinput1 and press enter it goes one space below and become like this
1
| 2
| 3
|
4
| 5
| 6
|
7
| 8
| 9
|
i tried to change sc.nextInt(); to sc.nextLine(); but it doesnt work even after i used parseInt ..
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int[][] noOfArrays;
int noOfRows;
int noOfColumns;
System.out.println("Output : ");
System.out.print("Input N = ");
noOfRows = sc.nextInt();
noOfColumns = noOfRows;
noOfArrays = new int[noOfRows][noOfColumns];
///////////////////////////////
for(int i = 0; i<noOfRows; i++)
{
for(int j=0; j<noOfColumns; j++)
{
noOfArrays[i][j] = sc.nextInt();
System.out.print(" | ");
}
System.out.println("");
}
}
There is no way by which you can detect the user key input on console using basic Java programming. If you need that functionality, you may need to use libraries like javacurses or JNI.
Two ways to handle this is:
[i, j]
|
and then read the whole line, split the value to arrays and form the row.