+ 4
W.A.P in Java to sort the array elements in descending order and ascending order ?Please someone tell me how to fix error.
8 Answers
+ 7
You have a problem with your for statements and you have it a few times:
for(j=i,j<n-1;j++)
Should be
for(j=i;j<n-1;j++)
And once I've fixed that, the Scanner has a problem...
I missed it, to use Scanner you need to import java.util.*;
+ 5
And you'll want to to figure out a way to populate your arrays, I imagine.
+ 5
Muqaddas Berdiqulova don't post things that aren't relevant to the post. That is spamming.
+ 3
Without the task description, input(s) and expected output(s), nobody understand what you want to achieve or where is the problem.
+ 3
Wong Hei Ming thanks for saying that now maybe I get the right code !
+ 3
Ausgrindtube thanks for finding the problem! Can you please help me in that if line , which is now causing trouble?
+ 2
I have a hard time to track your code. Without proper indentation it is difficult to spot nested blocks.
While I don't have much experience with Java, doesn't Java's Arrays has a sort() method?
Here is the link I found:
https://www.freecodecamp.org/news/java-sort-array-how-to-reverse-an-array-in-ascending-or-descending-order-with-arrays-sort-2/
Or is it you want to replicate the behavior in a hard way?
I ran your code at current state and find...
if(a[i]<[j+1])... missing a "a".
+ 2
A little bit formatting and some comments helps a lot to understand the code better.
Look:
https://code.sololearn.com/c5u1AM76Wdo6/?ref=app