To get your desired process, please simply change a[i]=sc.nextLine(); to a[i]=sc.next(); .Unless your string input has spaces in them, this should work fine, else place sc.nextLine(); before your for loop
this is how I see the problem;
nextInt(); expects and stores the first word of input as int, else an error is thrown, however, it doesn't break out of the current line.
So when you run n=sc.nextInt(); and gave the input
enter length of string
3
3 was saved to n but the system cursor still stays behind it like so, 3|,
N.B: the output stream is different from the input stream, so it doesn't matter that,System.out.println("enter string values");is run, the cursor for the input stream still stays behind 3;
enter length of string
3|
enter string values
a[i]=sc.nextLine() expects and stores the sentence after the cursor, else it breaks to the next line. But if the cursor is already on a new line, it gives the chance to enter a new sentence (then stores it if you want) and moves over to a new line.
so as there is no sentence after the cursor 3|, it breaks to the next line and assigns the break line (newline) command "\n" to a[0] in the for loop when i = 0,
so, since the cursor is now on a new line, it gives the option to enter a new sentence and stores it to a[1] when enter is tapped for i = 1 and moves to the next line, the same process is executed for when i = 2
then the contents of String[] a is displayed as follows;
enter length of string
3| //after, nextInt() when i=0, since the cursor is not the first, it breaks the line, a[0] = "n"
enter string values
|one //when i=1, since it's the firstit gives chance for input, and stores to a[1]
|two //when i=1, since it's the firstit gives chance for input, and stores to a[1]
//a[0] is displayed
one //a[1] is displayed
two //a[2] is displayed
I had fun giving my opinion on this, Thank You.
for(int i=0; i < a.length; i++)loops three times, from 0 to 2, which is correct.