W.A.P in Java to sort the array elements in descending order and ascending order ?Please someone tell me how to fix error. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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.

https://code.sololearn.com/c7Y42toIp1Vb/?ref=app

17th Oct 2023, 3:53 AM
Tanya Malhotra
Tanya Malhotra - avatar
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.*;
17th Oct 2023, 7:37 AM
Ausgrindtube
Ausgrindtube - avatar
+ 5
And you'll want to to figure out a way to populate your arrays, I imagine.
17th Oct 2023, 7:43 AM
Ausgrindtube
Ausgrindtube - avatar
+ 5
Muqaddas Berdiqulova don't post things that aren't relevant to the post. That is spamming.
18th Oct 2023, 5:50 AM
Ausgrindtube
Ausgrindtube - avatar
+ 3
Without the task description, input(s) and expected output(s), nobody understand what you want to achieve or where is the problem.
17th Oct 2023, 4:37 AM
Wong Hei Ming
Wong Hei Ming - avatar
+ 3
Wong Hei Ming thanks for saying that now maybe I get the right code !
17th Oct 2023, 4:42 AM
Tanya Malhotra
Tanya Malhotra - avatar
+ 3
Ausgrindtube thanks for finding the problem! Can you please help me in that if line , which is now causing trouble?
17th Oct 2023, 8:21 AM
Tanya Malhotra
Tanya Malhotra - avatar
+ 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".
17th Oct 2023, 8:36 AM
Wong Hei Ming
Wong Hei Ming - avatar
+ 2
A little bit formatting and some comments helps a lot to understand the code better. Look: https://code.sololearn.com/c5u1AM76Wdo6/?ref=app
17th Oct 2023, 12:03 PM
Stefanoo
Stefanoo - avatar