Stefan P Auckland Stefan P Auckland - 1 month ago 5
Java Question

Print array in a loop with dynamic variable Java

I am trying to define an array that increased by one each time then print the result of an equation that the array is assigned to;

my code so far is

public class PrimeNumberCounter {

public static void main(String[] args) {
// Define integers
int a = 1;
final int b = 2;
final int c = 3;
// Define Array
int[] nth;
nth = new int[100];
//Set loop
for (a = 1; a < 100; a = a + 1) {
int d = a * b + c;
System.out.println(d);
}
}
}


The math function and loop work but I can't figure out how to publish the nth term.
So far I have read array's are the most suitable solution but after much messing around removed the print array part, and decided to ask for help.

Thanks All

EDIT: addition of original code (it's kinda silly hence why I didn't publish it 1st time

public class PrimeNumberCounter {

public static void main(String[] args) {
// Define integers
int a = 1;
final int b = 2;
final int c = 3;
// Define Array.
int[] nth;
nth = new int[100];
//Set loop
for (a = 1; a < 100; a = a + 1) {
nth[] = a * b + c;
System.out.println(nth);

}
}
}

Answer

If I understood you correctly, you are not trying to increase the size of the array, what you are trying to do is increase a counter that iterates through the array and assigns the result of the d = a * b + c; equation to the ith element of the array. Then after all the elements in the array have been populated, you want to iterate through the array an print all the values. If that is the case you can modify your code as follows:

public class PrimeNumberCounter {

    public static void main(String[] args) {
        // Define integers
        int a = 1;
        final int b = 2;
        final int c = 3;
        //int d = 0; // not used
        // Define Array assuming that its size is fixed to 100
        int[] nth = new int[100];
        // Set the assignment loop
        for (a = 1; a <= nth.length; ++a) {
            // array indexing starts at 0
            nth[a - 1] = a * b + c;
        }
        // print loop
        for (int i = 0; i < nth.length; ++i) {
            System.out.printf("%d: %d\n", i + 1, nth[i]);
        }
    }
}

If you don't know the size of the array a priori you can use an ArrayList instead which can grow dynamically as mentioned in the comments and previous answer.

import java.util.List;
import java.util.ArrayList;

public class PrimeNumberCounter {

    public static void main(String[] args) {
        // Define integers
        int a = 1;
        final int b = 2;
        final int c = 3;
        // Some size N let's assume it's a 100 again
        List<Integer> nth = new ArrayList<Integer>();
        // Set the assignment loop
        for (a = 1; a <= 100; ++a) {
            // array indexing starts at 0
            nth.add(a - 1, a * b + c);
        }
        // print loop
        for (int i = 0; i < nth.size(); ++i) {
            System.out.printf("%d: %d\n", i + 1, nth.get[i]);
        }
    }
}
Comments