I am trying to code a program that asks the user to input 'n' words one on each line and gives the output as lexicographical sorted words. It asks the user to input the no. of words on 1st line and then the words one on each line. I am to use multi-dimensional arrays for this program. My code works fine except that it is unable to take 'n' no. of words as input by the user but instead takes only n-1 words. Kindly review my code and suggest the editing it needs. Thanks in advance.
The problem is that you are in fact getting n words, but one of them is the empty String "". Try
System.out.printf("Array length = %d%n", arr.length); as line 14½. You will see you get the correct length of array. Then try
System.out.printf("Number of characters in first entry is array = %d%n", arr.length()); which will probably show 0 characters.
Please tell us what you have been told Scanner#nextLine() does, because I bet it doesn't do what you think it does. Have a look at this post which gives a possible solution. Another possible solution is to recognise that there might be multiple words in each line and use next() instead of nextLine() throughout.
I am afraid there is a lot in your code.
It is all squashed too tightly together, so it is difficult to see where one part ends and the next begins.
Remove the java.io. import because you are not using it.
You should move all that code out of the main method. You should have separate methods (preferably in a separate class) for swapping two elements in the array like that, and another method (which uses the swap method) for that bubble‑sort. Don't declare the temporary variable there. Declare it inside the loop, so it will go out of scope sooner.
Put the  before the name of the array in its declaration.
There is no such thing as a multidimensional arra; Java® uses arrays of arrays. Please explain why you need an array of arrays.
Don't use a loop for printing an array like that. Try this instead:-
Thank you so much for your prompt and effective guidance. I agree I need a lot of improvement in making my code more optimized and elegant. I am just a beginner and I have a lot to learn and improve.
I finally figured out from your post that the error I got was mainly because of the nextLine().Replacing it with the next() solved the problem.
I used nextLine() in this code because from what I know, its meant for taking one string input from the user per line and that was what I intended to do.
I am to use multi-dimensional arrays for this program.
Why? There is no reason for multi-dimensional arrays here, and the code you have written does not use any multi-dimensional arrays.
After you input all of the words, and before you try to sort them, you should write out the inputs to see if you read them in properly.
then you can sort them and write out the sorted values. When you get that working, you can remove the code that writes the original values.
@Fred I agree there was no need of multi-dimensional arrays in this program. I was confused because the problem stated I 'could' use multi-dimensional arrays for it, though I wasn't sure how to actually implement it in this case.
@Campbell I saw the link posted by you. I now understand it was the
nextInt() that was creating an empty string. Inserting nextLine() immediately after nextInt() solved the empty string problem.