Ahmad Dawod
Ahmad Dawod

Reputation: 31

java display method

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;

            }

        }
    }
}

Upvotes: 0

Views: 4042

Answers (5)

Boris Pavlović
Boris Pavlović

Reputation: 64650

import java.util.Arrays;
import java.util.Random;

public class project2 {

  public static void main(String[] args) {

    int num[] = new int[100];

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

      num[i] = random.nextInt(100);
    }


    printArray(num);

    System.out.println('\n' + Arrays.toString(num));
  }

  public static void printArray(int array1[]) {

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

      System.out.printf("%2d ", array1[i]);
      if ((i + 1) % count == 0) {

        System.out.println("");
      }

    }
  }
}

Upvotes: 1

ppeterka
ppeterka

Reputation: 20736

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!

Upvotes: 0

Ajinkya
Ajinkya

Reputation: 22720

Problem is with
if (array1[x]==count)
You are comparing count with value present in array. Instead compare it with desired count ie 20 or Use modulo operator as suggested in other answers / comments .

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

    System.out.print(array1[x]+" ");

    if (count == 20){    // Check if its 20th element 
       System.out.println(" ");
       count=1;  // reset count 
    }
   count++;
}

Upvotes: 0

Azodious
Azodious

Reputation: 13882

i have to start a new line after every 20 elements.

Change to following code:

if (x%20 == 0)
{
    System.out.println();
}

in place of

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

Upvotes: 0

azraelAT
azraelAT

Reputation: 762

Take a closer look at your if-statement:

if (array1[x]==count)

According to your array values, this will never return true

Upvotes: 0

Related Questions