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 3443826
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T08:50:20+00:00 2026-05-18T08:50:20+00:00

I am trying to write a program that will, when executed, will go through

  • 0

I am trying to write a program that will, when executed, will go through an array and remove all instances of 0.0 and change the size of the array equal to the number of non zero elements and put those elements in their previous order. That is, if n=10 and the contents of a[j], j = 0 to n – 1 are initially

0.0, 1.2, 0.0, 0.0, 0.0, 2.3, 0.0, 9.7, 5.6, 0.0

then after execution of the code the contents should be

 n=4, a[0]=1.2, a[1]=2.3, a[2]=9.7, and a[3]=5.6.

This is what I have so far:

import java.util.Scanner;
public class hw2
{
   public static void main(String[] args) 
   {
       Scanner scan = new Scanner(System.in);
       final double KEY = 0.0;
       int n = scan.nextInt();
       double[] a = new double[n];
       for(int i=0; i<n; i++)
       {
           a[i] = scan.nextDouble();
       }
       for(int k = 0; k<n; k++)
       {
           if(a[k] == KEY)
           {
               a[k] = a[k+1];
               n--;
           }
           System.out.println(a[k]);
       }
   }
}

Just a little nudge in the right direction would be appreciated.

  • 1 1 Answer
  • 0 Views
  • 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-18T08:50:20+00:00Added an answer on May 18, 2026 at 8:50 am

    Your implementation (2nd for loop) is not right, it will fail to simple test case:
    Input > 5 2.0 2 0.0 3 0.0
    Your program will have wrong output:
    2.0
    2.0
    3.0
    3.0

    but it should be 2.0 2.0 3

    Also, you can’t use == to compare two double.

    The following code is my solution basing on your current code:

        public class hw21 {
        public static void main(String[] args) {
            Scanner scan = new Scanner(System.in);
            final double KEY = 0.0;
            final Double ACCEPTABLE_TOLERANCE = 0.000000000001d;
    
            int n = scan.nextInt();
            double[] a = new double[n];
            for (int i = 0; i < n; i++) {
                a[i] = scan.nextDouble();
            }
            for (int k = 0, j = 0; k < n; k++) {
                if (Math.abs(a[k] - KEY) < ACCEPTABLE_TOLERANCE) {
                    continue;
                }
                a[j] = a[k];
                System.out.println(a[j]);
                j++;
            }
        }
    }
    

    Also I prefer to use an ArrayList like below:

    public class hw2 {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        final double KEY = 0.0;
        final Double ACCEPTABLE_TOLERANCE = 0.000000000001d;
    
        int n = scan.nextInt();
        double[] a = new double[n];
        for (int i = 0; i < n; i++) {
            a[i] = scan.nextDouble();
        }
    
        List<Double> newList = new ArrayList<Double>();
        for (int k = 0; k < n; k++) {
            if (Math.abs(a[k] - KEY) < ACCEPTABLE_TOLERANCE) {
                continue;
            }
            newList.add(a[k]);
        }
    
        System.out.println("There are " + newList.size() + " no-zero double:");
            System.out.println(newList);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to write a program that will change a particular GUID in a
I'm trying to write a program that will loop through cells of a specific
I am trying to write a program that will read all of the words
I am trying to write a program that will prompt you to enter someone's
Hey there. I'm trying to write a small program that will read the four
I'm trying to write a procedure for a baseball program that will use multiple
I am trying to write a simple program in C# that will read data
I'm trying to write a simple program that will receive a string of max
I am trying to write a java program that will automatically download and name
I am trying to write a program that will output via coordinates. I am

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.