Question

I am taking a programming class, and things just aren't clicking for me. I have an assignment that asks to:

Write a program to assign the integer values 1 through 25 to a 25 element integer array. Then, print the array as five separate lines each containing five elements separated by commas. The last element on each line should be followed by a newline instead of a comma. The output of your program should appear exactly as follows:

1,2,3,4,5

6,7,8,9,10

11,12,13,14,15

16,17,18,19,20

21,22,23,24,25

Hints:

  • One way to determine every 5th element is to use the modules operator (%). If you divide the subscript by 5 and the remainder is 0, it is the 5th number.
  • You can use System.out.print() to print a value without a newline following it. This will allow you to print multiple things on the same line.

I have a little bit of code but I don't know where to go from here:

public class Program4

{

     public static int[] array;

     public static void main(String[] args);

     {

          int[] numbers = new int [25]

          for(int i=0; i<25; i++)

                array[i] = i + 1;}

     public static void printArray()

     {

          for(int i=1; i<=25; i++);

          {

               System.out.print(array[i - 1]);

               if (i % 5 == 0)

                    System.out.printIn();

           }

     }

I just have a mental block about programming-can anyone help point me to some helpful examples?

Was it helpful?

Solution

Try this,

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
     public static int[] array;

     public static void main(String[] args)
     {
        array = new int[25];
        for(int i=0; i<25; i++)
           array[i] = i + 1;   
        printArray();
     }

     public static void printArray()

     {
        int i;
        for(i=1; i<=25; i++){
            if (i % 5 != 0)
                System.out.print(array[i-1]+",");
            else
                System.out.println(array[i-1]);
       }
     }
}

OTHER TIPS

public class Foo {

    public static int[] nums;

    public static void main(String[] args) {
        nums = new int[25];
        for (int i = 0; i < nums.length; i++) {
            nums[i] = i + 1;
        }
        printArray(nums);
    }

    public static void printArray(int[] myArray) {
        for (int i = 0; i < myArray.length; i++) {
            System.out.print(String.valueOf(myArray[i]);
            if (i % 5 == 0) {
                System.out.println();
            } else if (i % 5 != 4){
                System.out.println(", ");
        }
    }
}
public class Test {
    public static void main(String[] args) {

        int[] array = new int [25];

        for(int i=0; i<25; i++) {
            array[i] = i + 1;
        }


        for (int i=1; i<=25; i++) {
            System.out.print(array[i - 1]);
            if (i % 5 == 0) {
                System.out.println();
            } else {
                System.out.print(", ");
            }
        }
    }
}

And try to learn Java syntax first of all.

Here is an enhanced version of your code.

public class Program4

{

public static int[] array = new int[25];//instantiate the array to its default values

public static void main(String[] args)
{
    //calling the methods from main
    addToArray();
    printArray();
}
//add the numbers to the array
public static void addToArray(){



    for(int i=0; i<25; i++)

        array[i] = i + 1;

}

 //print the numbers from the array

public static void printArray()
{
  for(int i = 1; i <= 25; i++){
      if(i % 5 == 0){
          System.out.print(i);
          System.out.println();
      }
      else{
          System.out.print(i + ",");
      }
  }

}

}

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top