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:

  • As user for input on [i, j]
  • Prompt user for input separated by | and then read the whole line, split the value to arrays and form the row.
  • 链接地址: http://www.djcxy.com/p/96116.html

    上一篇: 线程“main”中的异常java.util.NoSuchElementException错误

    下一篇: 如何在2dArray循环中的一行中插入多个输入