Loop Through Array for Positive Check
In this step, we will learn how to iterate through an array of numbers in Java and check if each number is positive. This is a fundamental operation in programming, allowing us to process collections of data.
First, let's create a new Java file named PositiveCheck.java
in your ~/project
directory. You can do this by right-clicking in the File Explorer on the left and selecting "New File", then typing PositiveCheck.java
.
Now, open the PositiveCheck.java
file in the editor and add the following code:
public class PositiveCheck {
public static void main(String[] args) {
// Define an array of integers
int[] numbers = {10, -5, 20, 0, 15, -8};
// Loop through the array
for (int i = 0; i < numbers.length; i++) {
// Get the current number
int currentNumber = numbers[i];
// Check if the number is positive
if (currentNumber > 0) {
System.out.println(currentNumber + " is a positive number.");
}
}
}
}
Let's break down this code:
public class PositiveCheck
: This declares our class, matching the file name.
public static void main(String[] args)
: This is the main method where our program execution begins.
int[] numbers = {10, -5, 20, 0, 15, -8};
: This line declares an array of integers named numbers
and initializes it with some values. An array is a collection of elements of the same data type.
for (int i = 0; i < numbers.length; i++)
: This is a for
loop. It's a control structure that allows us to repeat a block of code multiple times.
int i = 0
: This initializes a counter variable i
to 0.
i < numbers.length
: This is the condition that is checked before each iteration. The loop continues as long as i
is less than the length of the numbers
array. numbers.length
gives us the number of elements in the array.
i++
: This increments the counter i
by 1 after each iteration.
int currentNumber = numbers[i];
: Inside the loop, this line accesses the element at the current index i
from the numbers
array and stores it in a variable called currentNumber
. Array indices start from 0.
if (currentNumber > 0)
: This is an if
statement. It checks if the currentNumber
is greater than 0.
System.out.println(currentNumber + " is a positive number.");
: This line is executed only if the if
condition is true (i.e., the number is positive). It prints the positive number followed by the text " is a positive number.".
Save the PositiveCheck.java
file (Ctrl+S or Cmd+S).
Now, let's compile and run this program in the Terminal. Make sure you are in the ~/project
directory.
Compile the code:
javac PositiveCheck.java
If there are no errors, a PositiveCheck.class
file will be created.
Now, run the compiled code:
java PositiveCheck
You should see the output showing only the positive numbers from the array.
10 is a positive number.
20 is a positive number.
15 is a positive number.
This demonstrates how to use a for
loop to iterate through an array and apply a condition to each element. In the next step, we will explore a more modern way to achieve a similar result using Java's Stream API.