package com.codegym.task.task07.task0728;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.Collections;
/*
In decreasing order
*/
public class Solution {
public static void main(String[] args) throws Exception {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
Integer[] array = new Integer[20];
for (int i = 0; i < 20; i++) {
array[i] = Integer.parseInt(reader.readLine());
}
// sort(array);
Arrays.sort(array, Collections.reverseOrder());
for (int x : array) {
System.out.println(x);
}
}
public static void sort(Integer[] array) {
//write your code here
for (int x : array) {
System.out.println(x);
}
}
}
Input ==
1
2
3
4
5
6
7
9
8
12
11
14
43
54
666
7777
88888
999999
6767
565
Output ==
999999
88888
7777
6767
666
565
54
43
14
12
11
9
8
7
6
5
4
3
2
1
The Code works, but does not satisfy the conditions. Wondering if there is a way to make this particular code work + satisfy the conditions.........Just looking for the shortest and fastest solution......
Resolved
Comments (9)
- Popular
- New
- Old
You must be signed in to leave a comment
Guadalupe Gagnon
10 December 2019, 18:46
you need to share your solution or we can't help
0
Kris
11 December 2019, 15:45
Sorry, I though the code is visible if I paste it in the comments ==>>>
package com.codegym.task.task07.task0728;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.Collections;
/*
In decreasing order
*/
public class Solution {
public static void main(String[] args) throws Exception {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
Integer[] array = new Integer[20];
for (int i = 0; i < 20; i++) {
array[i] = Integer.parseInt(reader.readLine());
}
// sort(array);
Arrays.sort(array, Collections.reverseOrder());
for (int x : array) {
System.out.println(x);
}
}
public static void sort(Integer[] array) {
//write your code here
for (int x : array) {
System.out.println(x);
}
}
}
Input ==
1
2
3
4
5
6
7
9
8
12
11
14
43
54
666
7777
88888
999999
6767
565
Output ==
999999
88888
7777
6767
666
565
54
43
14
12
11
9
8
7
6
5
4
3
2
1
0
Kris
11 December 2019, 15:51
btw I really like this solution, because it is very short even if the conditions do not :)
0
Guadalupe Gagnon
11 December 2019, 15:55
This is sharing the solution:
0
Guadalupe Gagnon
11 December 2019, 15:59
It looks like the code you copied + pasted, while it does sort numbers, does not implement the sort method. If I used your sort method:
All it would do is output the array in the exact order that it is passed in. It should be able to take in any array and sort it properly. 0
Kris
11 December 2019, 16:44
1) I just thought, that if I paste the code in the notes it will be shown to you also, so I did not used the attach option in order not to double the code -->>>
That was my mistake, I will use the attach function from now on. Thanks for pointing this to me.
2) Yes I tried to implement collections = Arrays.sort(array, Collections.reverseOrder()); in the sort method, but I always got an error of some kind. I have the strong feeling I have to read more about collections first :D:D
Thanks
0
Guadalupe Gagnon
11 December 2019, 17:41useful
collections = Arrays.sort(array, Collections.reverseOrder());
This is not a valid java statement. The code for the sort method should look like this:
reset the task and only add this in and you will pass.
+1
Kris
12 December 2019, 13:17
Thanks mate.
I reset the task and this are the results ==
The code works as expected, but the last 3 conditions are still not satisfied. I think it is related to the int vs Integer struggle.
1) If I use
int[] array = new int[20];
public static void sort(int[] array) {
Arrays.sort(array, Collections.reverseOrder());
I get an error message like == no suitable method for sort int ........................
2) If I use
Integer[] array = new Integer[20];
public static void sort(Integer[] array) {
Arrays.sort(array, Collections.reverseOrder());
}
}
All works except does not satisfy last 3 conditions
0
Guadalupe Gagnon
12 December 2019, 14:24solution
Ok, I didn't realize that the task wants you to sort an int array and not an Integer array. Integer and int, while having similar functions, are not the same. Integer is an object type that wraps an int and supports the collections interface, while ints are primitives that do not do so by themselves. There is quite a distinction there. With an int array you can use the Arrays.sort() method, but not the Collections.reverseOrder() with it. So that will solve the first part by There is no "easy" inbuilt way to reverse an int array in java, you will have to
A) convert the array to an appropriate object array, reverse the order, then convert back;
or B) write your own code to reverse, which plenty of information on the internet can help you with.
+2