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

  • Home
  • SEARCH
  • 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 9182579
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T18:35:34+00:00 2026-06-17T18:35:34+00:00

Here im required to Write a method printArray that displays the contents of the

  • 0

Here im required to Write a method printArray that displays the contents of the array num and Display the contents of the array with each
number separated by a space. and i have to start a new line after every 20 elements.

i wrote this code but whenever i try to execute it, it shows the array without the new line

public class project2 {

    public static void main(String[] args) {

        int num []= new int [100];

        for (int i=0;i<num.length;i++){

            num[i]=-1;
            num[7]=7;
        }


        printArray(num);
        System.out.println(num);


    }

    public static void printArray (int array1[]){

        int count =20;
        for (int x=0;x<array1.length;x++){

            System.out.print(array1[x]+" ");
            if (array1[x]==count){

                System.out.println(" ");
                count=array1[x]+count;

            }

        }
    }
}
  • 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-17T18:35:36+00:00Added an answer on June 17, 2026 at 6:35 pm

    You should use the modulo (or remainder) operator (%), that suits your usage much better:

    for (int x=0;x<array1.length;x++){
        System.out.print(array1[x]+" ");
        if (x>0 && (x%count)==0){
            System.out.println(" ");
        }
    
    }
    

    This way, you will get a new line every count characters, and the first line will not have it (that is why the x>0 check is there).

    Also, in the original post, this line is frankly totally bad:

    count=array1[x]+count;
    

    Just what would it do? Why do you add the value stored in the array to the fixed counter? Considering this line, I advise that you should really sit back a bit, and try to think about how things work in the background… There is no magic!

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

Sidebar

Related Questions

We are following below practices to write JUnit tests for our methods. Each method
My project has required this enough times that I'm hoping someone on here can
I am trying to write a generic method that will convert a DataTable to
I always get back Content Length Required 411, what am I doing wrong here?
I've never actually had an instance where this was required, but here I am...
Look at this: $('form).submit(function (event) { $(':input.required').trigger('blur'); var num = $('.warning', this).length; alert(num);//Focus here!
I have a problem here that requires to design a data structure that takes
I'm trying to write a method which takes a Map[K, Collection[V]] and converts it
OK, I know what you're thinking, "why write a method you do not want
I have a method that is supposed to return an Int. I am trying

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.