Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1089213
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T23:11:15+00:00 2026-05-16T23:11:15+00:00

package hw3; public class Main { public static void main(String[] args) { final int

  • 0
package hw3;

public class Main {
    public static void main(String[] args) {
        final int NumberOfElements = 1000;
        int[] num = new int[NumberOfElements];
        int var = 0;
        //create input
        java.util.Scanner input = new java.util.Scanner(System.in);
        for (int i = 0; i < NumberOfElements; i++) {
            System.out.print("Enter any positive number or enter 0 to stop: ");
            num[i] = input.nextInt();
            var++; 
            if (num[i] == 0)

                break;

            }
             Arrays.sort( num, 0, var);
             int i;
             for (i = 0; i < var; i++) {
             System.out.print("   " + num[i]);


        }
    }
}

Write a Java program reading a sequence of positive integers entered one per line. The program stops reading when integer ‘0’ is entered. The program will sort and output them into an ascending numerical order.For example: 5
1
7
12
36
8
0
Output: 1 5 7 8 12 36

  • 1 1 Answer
  • 1 View
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T23:11:16+00:00Added an answer on May 16, 2026 at 11:11 pm

    Try to understand the problem: there are three steps.
    Step 1: Get the input from the user. Continue getting input till the user enters 0
    Step 2: Sort
    Step 3: Print out the results

    Step 1: Your for loop is almost correct.
    But the for loop should end immediately after you have got the input.
    In the following code,

       num[i] = input.nextInt();
       var++; 
       if (num[i] == 0)
          break;
    

    you are adding the user input to the array and then checking whether it is 0. This means that the 0 will also be part of your array. If you don’t want this, you should check the input and add it to the array only if it is not 0.

    Also note the declaration of i before the for loop. Because we need it after the for loop. Why? see below.

    int i = 0; 
    for (i = 0; i < NumberOfElements; i++) {
       int n = input.nextInt();
        if (n == 0)
           break;
        else
           num[i] = n;
    } //for loop ends here
    

    Step 2: Sort it
    Arrays.sort(num);

    Step 3: Print the output:

    for (i = 0; i < num.length; i++) {
          System.out.print("  " + num[i]);
       }
    

    The problem is, in step 2, an array of 1000 elements is sorted, while you actually need to consider only the number of elements the user has entered. You don’t know that initially thats why you created an array of 1000 elements.But, at this point (after step 2) you do know how many number of elements the user has entered. This is present in i

    So new step between 1 and 2: Create a new arrays that contains only those elements that the user has entered.
    Step 1.5:

    int[] newArray = Arrays.copyOf(num, i);
    

    Now sort this new array, and print it (same as your code, but uses the new array we just created)

    Arrays.sort(newArray);

    for (i = 0; i < newArray.length; i++) {
          System.out.print("  " + newArray[i]);
       }
    

    Notes:
    1. The ideal way to do this is to use Lists and not arrays, but probably since this is homework, you might have to use arrays.

    1. Since this is homework, I don’t know whether you are allowed to use Arrays.sort or Arrays.copy. Your professor might frown at this because perhaps his intention was that you learn the constructs of the language via for, if and while. In that case you have to do step 1.5 (make array the right size) and sort by yourself.
      This is not difficult (but just remember this is not the best way to do it, except for in a homework)

    Copy array (homemade) (in place of step 1.4 above)

    int[] newArray = new int[i]
    for(int j=0; j<i; j++){
       newArray[j] = num[j];
    }
    

    Sort (homemade) (in place of step 2 above):

    1. Loop through the elements
    2. If one element is greater than the previous element, swap them (in asc order, the prev element is always lesser or equal to the next element)

    There are two loops because you have to do the comparison on a continuous basis: the the first element, compare with the entire array, place it in the right position, take the second compare with the entire array etc…)

      for(int j=0; j<newArray.length; j++) {
          for(int k=0; k<newArray.length; k++) {
             if(newArray[k] > newArray[j]) { 
                //the swap logic:
                int t = newArray[k];
                newArray[k] = newArray[j];
                newArray[j] = t;
             }
          }
       }
    

    Try to understand what really is happening, instead of just copy pasting.
    Once you understand the homemade sort logic, think about this:
    The second for loop in the sort or(int k=0; k<newArray.length; k++) { can actually just be for(int k=0; k<newArray.length; k++) {. Why?

    Your print loop will remain the way you wrote it, but you will print newArray rather than num. You might want to change the loop variable to int j or something, but i will also work. (i holds the no of inputs now, so I would not use it for any other purpose. But it is just a manner of coding. Technically no difference – the code will work the same way)

    I am not combining the parts. I leave that o you, otherwise it will look like I did your homework 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

package testing.project; public class PalindromeThreeDigits { public static void main(String[] args) { int value
package javaapplication8; public class Main { public static void main(String[] args) { int[] list1
package org.study.algos; public class Study { public static void main(String[] args) { A a
package GC; import java.util.Scanner; public class main { public static void main(String args[]) {
package com.test01; public class test01 { public static void main(String[] args) { System.out.println(hi); }
package pack; public class sample{ public static void main(String input[]) { NumberFormat numberFormat =
package matlab; import com.mathworks.toolbox.javabuilder.*; import com.eigenface.Eigenface; public class Test { public static void main(String[]
package com.valami; public class Ferrari { private int v = 0; private void alam()
package javaapplication1; import java.io.*; public class Main { /** * @param args the command
package pkg_1; public class ExpOnWaitMethod extends Thread { static Double x = new Double(20);

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.