Q:

How does nextLine work with scanner?

I am trying to split "sisestus" into different words by using space as a separator, but by just using sc.next() doesn't allow me to enter a string with spaces so i read that i should use .nextLine(), but it doesn't work at all. How do i solve it?
public class Sisestamine {
int read;
int veerud;
double Maatriks[][];
java.util.Scanner sc = new java.util.Scanner(System.in);

Sisestamine() {
    System.out.println("Enter matrix dimensions (format NxM)");

    String[] abi = sc.next().split("x");
    this.read = Integer.parseInt(abi[0]);
    this.veerud = Integer.parseInt(abi[1]);
    this.Maatriks = new double[read][veerud];

    System.out.println("Enter the matrix: (x x x x etc..)");
    for (int i = 0; i < read; i++) {
        String sisestus = sc.next();
                  //String sisestus = sc.nextLine(); 
        abi = sisestus.split(" "); 
        System.out.print(abi);
        for (int j = 0; j < abi.length; j++) {
            this.Maatriks[i][j] = Double.parseDouble(abi[j]);
        }
     }
   }
 }

A:

Your problem could be because you're using sc.next() at one part of your program and then sc.nextLine() later, and the second call swallows the end of line (EOL) token that was left hanging from the prior call to next(). A solution is to use sc.nextLine() for all invocations. In other words, change all sc.next() to sc.nextLine().

