Of course. The input "2 6 3 2 5" represents a sequence of numbers. In Java, the most common way to handle a list of numbers like this is with an array or an ArrayList.

Here are several examples showing what you can do with these numbers in Java, from the most basic to more practical applications.
Storing the Numbers in an Array
This is the most fundamental approach. An array is a fixed-size container to hold a sequence of elements of the same type.
public class NumberSequence {
public static void main(String[] args) {
// The numbers from your input
int[] numbers = {2, 6, 3, 2, 5};
// You can access individual elements using their index (starting from 0)
System.out.println("The first number is: " + numbers[0]); // Output: 2
System.out.println("The third number is: " + numbers[2]); // Output: 3
// You can loop through all the numbers
System.out.println("\nAll numbers in the array:");
for (int i = 0; i < numbers.length; i++) {
System.out.print(numbers[i] + " "); // Output: 2 6 3 2 5
}
}
}
Common Operations on the Numbers
Here are some very common tasks you would perform on this sequence of numbers.
a) Finding the Sum
public class SumOfNumbers {
public static void main(String[] args) {
int[] numbers = {2, 6, 3, 2, 5};
int sum = 0;
for (int number : numbers) {
sum += number; // Add each number to the sum
}
System.out.println("The numbers are: " + java.util.Arrays.toString(numbers));
System.out.println("The sum is: " + sum); // Output: 18
}
}
b) Finding the Average
public class AverageOfNumbers {
public static void main(String[] args) {
int[] numbers = {2, 6, 3, 2, 5};
int sum = 0;
for (int number : numbers) {
sum += number;
}
double average = (double) sum / numbers.length; // Cast to double for decimal result
System.out.println("The numbers are: " + java.util.Arrays.toString(numbers));
System.out.println("The average is: " + average); // Output: 3.6
}
}
c) Finding the Maximum and Minimum Values
public class FindMinMax {
public static void main(String[] args) {
int[] numbers = {2, 6, 3, 2, 5};
// Initialize max and min with the first element
int max = numbers[0];
int min = numbers[0];
for (int number : numbers) {
if (number > max) {
max = number;
}
if (number < min) {
min = number;
}
}
System.out.println("The numbers are: " + java.util.Arrays.toString(numbers));
System.out.println("The maximum value is: " + max); // Output: 6
System.out.println("The minimum value is: " + min); // Output: 2
}
}
d) Counting Occurrences of a Specific Number
Let's count how many times the number 2 appears.

public class CountOccurrences {
public static void main(String[] args) {
int[] numbers = {2, 6, 3, 2, 5};
int targetNumber = 2;
int count = 0;
for (int number : numbers) {
if (number == targetNumber) {
count++;
}
}
System.out.println("The numbers are: " + java.util.Arrays.toString(numbers));
System.out.println("The number " + targetNumber + " appears " + count + " times."); // Output: 2
}
}
Reading Input from the User
Often, you won't have the numbers hardcoded. You'll want to get them from the user. This example reads space-separated numbers from the console.
import java.util.Scanner;
import java.util.Arrays;
public class ReadInput {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter numbers separated by spaces (e.g., 2 6 3 2 5):");
String inputLine = scanner.nextLine();
// Split the string by spaces to get individual number strings
String[] numberStrings = inputLine.split(" ");
// Create an integer array to store the numbers
int[] numbers = new int[numberStrings.length];
// Convert each string to an integer and store it in the array
for (int i = 0; i < numberStrings.length; i++) {
numbers[i] = Integer.parseInt(numberStrings[i]);
}
scanner.close();
// Now you can use the 'numbers' array
System.out.println("Successfully read the numbers: " + Arrays.toString(numbers));
}
}
Using an ArrayList (More Flexible)
An ArrayList is like an array, but its size can change dynamically. It's often more convenient.
import java.util.ArrayList;
import java.util.Arrays;
public class ArrayListExample {
public static void main(String[] args) {
// Create an ArrayList of Integers
ArrayList<Integer> numbersList = new ArrayList<>(Arrays.asList(2, 6, 3, 2, 5));
System.out.println("Initial list: " + numbersList);
// Add a new number
numbersList.add(10);
System.out.println("After adding 10: " + numbersList);
// Remove a number (the first occurrence of '2')
numbersList.remove(Integer.valueOf(2));
System.out.println("After removing the first 2: " + numbersList);
// Get the size of the list
System.out.println("The size of the list is: " + numbersList.size());
}
}
Summary
| Task | Best Java Tool | Why? |
|---|---|---|
| Storing a fixed sequence | int[] array |
Simple, fast, and memory-efficient. The size is fixed. |
| Storing a sequence that might change | ArrayList<Integer> |
Dynamic size. Easy to add or remove elements. |
| Reading input from a user | Scanner class |
The standard way to get input from the console. |
| Performing math operations | A simple for loop |
Gives you full control over the logic. |
| Finding min/max/count | A for loop |
Easy to implement and understand. |

