CodeGym
Promotion
CodeGym University
Learning
Course
Tasks
Surveys & Quizzes
Games
Help
Schedule
Community
Users
Forum
Chat
Articles
Success stories
Activity
Reviews
Subscriptions
Light theme
Question
  • Reviews
  • About us
Start
Start learning
Start learning now
  • All questions
Juan Gallardo
Level 10
Edmonton
  • 13.11.2018
  • 1202views
  • 5comments

Another code that should be passing :)

Question about the task In decreasing order
Java Syntax,  Level 7,  Lesson 12
Under discussion

Task: Write a program that reads 20 numbers from the keyboard and displays them in decreasing order.

Requirements:
  • The program should read 20 integers from the keyboard.
  • The program should display 20 numbers.
  • The Solution class must have a public static void sort(int[] array) method.
  • The main method should call the sort method.
  • The sort method should sort the passed array in decreasing order.
package com.codegym.task.task07.task0728; import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.Arrays; public class Solution { public static void main(String[] args) throws Exception { BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); int[] array = new int[20]; for (int i = 0; i < 20; i++) { array[i] = Integer.parseInt(reader.readLine()); } sort(array); } public static void sort(int[] array) { Arrays.sort(array); int[] array2 = new int[20]; for (int i = array.length-1; i >= 0; i--) { array2[array2.length-i-1] = array[i]; } for (int x : array2) { System.out.println(x); } } }
0
Comments (5)
  • Popular
  • New
  • Old
You must be signed in to leave a comment
towelie8
Level 16 , Darmstadt, Germany
27 August 2020, 10:07
public static void sort(int[] array) { //write your code here for (int i = 0; i < array.length; i++){ array[i] = array[i] * -1; } Arrays.sort(array); for (int i = 0; i < array.length; i++){ array[i] = array[i] * -1; } }
0
Girish Patel
Level 8 , Hyderabad, India
16 October 2019, 18:42
You don't need to print array in sort method print array in main method. And also need to change the sort method.
public static void main(String[] args) throws Exception {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        int[] array = new int[20];

        for (int i = 0; i < 20; i++) {
            array[i] = Integer.parseInt(reader.readLine());
        }
        sort(array);
        for (int x : array) {
            System.out.println(x);
        }
    }

    public static void sort(int[] array) {
        Arrays.sort(array);
        int[] array2 = new int[20];

        for (int i = array.length-1; i >= 0; i--) {
            array2[array2.length-i-1] = array[i];
        }

        for (int i = 0; i < 20; i++) {
            array[i] = array2[i];
        }
    }
0
Michael Martin
Level 19 , Arlington, United States
13 November 2018, 16:49
In your program, you are sorting the passed array in increasing order. You need to sort the array in DECREASING order, not instantiate a new array and sort that one.
+1
Juan Gallardo
Level 10 , Edmonton, Canada
14 November 2018, 02:32
But when I make the sort() method return a sorted array, I get an error because the method is supposed to be void!
0
Luis Rodriguez Ugarte
Level 22 , Chatillon-sur-Chalaronne, France
19 September 2019, 12:29
You don't have to print anything in sort method
0
Learn
  • Registration
  • Java Course
  • Help with Tasks
  • Pricing
  • Game Projects
  • Java Syntax
Community
  • Users
  • Articles
  • Forum
  • Chat
  • Success Stories
  • Activity
  • Affiliate Program
Company
  • About us
  • Contacts
  • Reviews
  • Press Room
  • CodeGym for EDU
  • FAQ
  • Support
CodeGym CodeGym is an online course for learning Java programming from scratch. This course is a perfect way to master Java for beginners. It contains 1200+ tasks with instant verification and an essential scope of Java fundamentals theory. To help you succeed in education, we’ve implemented a set of motivational features: quizzes, coding projects, content about efficient learning and Java developer’s career.
Follow us
Interface language
Programmers Are Made, Not Born © 2023 CodeGym
MastercardVisa
Programmers Are Made, Not Born © 2023 CodeGym
This website uses cookies to provide you with personalized service. By using this website, you agree to our use of cookies. If you require more details, please read our Terms and Policy.