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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T09:27:11+00:00 2026-06-17T09:27:11+00:00

The assignment given was, write a program that reads numbers off of a file,

  • 0

The assignment given was, write a program that reads numbers off of a file, construct an array from those numbers, and move all the zeroes to the end of the array.

For example.

Before: 0, 9, 7, 0, 0, 23, 4, 0

After: 9, 7, 23, 4, 0, 0, 0, 0

After toying with it for about 2 hours i came up with this.

import java.io.*;
import java.util.Scanner;

public class Compactor{

Scanner in;
private int numNum = 0;

public void calcNumNum(){
    try{
        in = new Scanner(new File("compact.txt"));
        while(in.hasNext()){
            int dumpVal = in.nextInt();
            numNum++;
        }
        makeArray(numNum);
    }catch(IOException i){
        System.out.println("Error: " + i.getMessage());
    }
}

private void makeArray(int x){
    int i = 0;
    int[] arrayName = new int[x];
    try{
        in = new Scanner(new File("compact.txt"));
        while(i < x){
            arrayName[i] = in.nextInt();
            i++;
        }
        compact(arrayName);
    }catch(IOException e){
        System.out.println("Error: " + e.getMessage());
    }
}

private void compact(int[] x){
    int counter = 0;
    int bCounter = (x.length - 1);
    for(int j = 0; j < x.length; j++){
        if(x[j]!=0){
            x[counter] = x[j];
            counter++;
        }else{
            x[bCounter] = x[j];
            bCounter--;
        }
    }
    printArray(x);
}

private void printArray(int[] m){
    int count = 0;
    while(count < m.length){
        System.out.print(m[count] + "  ");
        count++;
    }
}

}

The file that was given to us was: 0, 6, 13, 0, 0, 75, 33, 0, 0, 0, 4, 2,9 21, 0, 86, 0, 32, 66, 0, 0.

What i got was: 6, 13, 75, 33, 4, 29, 21, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0. (without the commas of course, i just put those in for easier reading.)

Could anyone perhaps give me insight on how to fix this problem, or maybe i should just start my code over with a different approach, the whole,

if(x[j]!=0){
    x[counter] = x[j];
    counter++;
 }else{
    x[bCounter] = x[j];
    bCounter--;
 }

i just made it up on the fly, thinking it would work fine, obviously it kept on going after it got past the last value and kept setting more and more values counting backwards as zeroes, no idea how to make it work though, any help would be greatly 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-06-17T09:27:12+00:00Added an answer on June 17, 2026 at 9:27 am

    You’re almost there with compact():

    private void compact(int[] x) {
        int counter = 0;
        for (int j = 0; j < x.length; j++) {
            if (x[j] != 0) {
                x[counter++] = x[j];
            }
        }
        while (counter < x.length) {
            x[counter++] = 0;
        }
        printArray(x);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was given an assignment to write a program to print only even numbers
A while back we were given an assignment to write a c program that
i was given an assignment . Write a program to simulate job scheduling in
Assignment: Write a C++ program to read the menu information from menu.txt. First letter
Recently for a programming class, we were given the assignment to write a program
I'm trying write a C program that sends an UDP packet to a given
My class assignment is to write a program that has the user input a
I was given the following HomeWork assignment, Write a program to test on your
Good day, I have been given an assignment that gets data off a CSV
I have been given the following assignment: Write a function (documentation included) that given

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.