• Post Reply Bookmark Topic Watch Topic
  • New Topic

Editing the java code for lexicograhical order of user input no. of words  RSS feed

 
Khusbu Sinha
Ranch Hand
Posts: 117
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
     
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

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[0].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 shou‍ld move all that code out of the main method. You shou‍ld 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:-
 
Khusbu Sinha
Ranch Hand
Posts: 117
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Khusbu Sinha wrote:. . . nextLine() . . . its meant for taking one string input from the user per line and that was what I intended to do.
If you look at the link I posted earlier today, you will find that is not quite correct.
 
Fred Kleinschmidt
Bartender
Posts: 571
9
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Khusbu Sinha
Ranch Hand
Posts: 117
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@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.
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Khusbu Sinha wrote:. . . the problem stated I 'could' use multi-dimensional arrays for it . . .
That is different from what you said at first. I cannot see that a String[] would be useful.
nextInt() that was creating an empty string. . . .
Yes, that is the problem. The nextLine method reads from the current position to the end of the line; after nextInt that often returns the empty String.
 
Fred Kleinschmidt
Bartender
Posts: 571
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:I cannot see that a String[] would be useful
Campbell, I assume you meant "String[][]"?
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Fred Kleinschmidt wrote:. . . Campbell, I assume you meant "String[][]"?
Yes, you're right.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!