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
  • Instead of 20 first go for 3 and work it out in your head. Do you know the modulo operator % ? Commented Jan 24, 2013 at 9:16

5 Answers 5

1
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("");
      }

    }
  }
}
Sign up to request clarification or add additional context in comments.

Comments

0

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!

Comments

0

Take a closer look at your if-statement:

if (array1[x]==count)

According to your array values, this will never return true

Comments

0

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;
}

Comments

0

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++;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.