-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArrayReplacement.java
More file actions
35 lines (29 loc) · 1.28 KB
/
ArrayReplacement.java
File metadata and controls
35 lines (29 loc) · 1.28 KB
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
26
27
28
29
30
31
32
33
34
import java.io.*;
public class ArrayReplacement {
public static void main(String[] args) throws IOException {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter your 10 numbers (space-separated): ");
// Read a line of input and split it into an array of strings
String[] inputValues = bufferedReader.readLine().split(" ");
// Create an array to store integers
int[] X = new int[10];
// Convert the string values to integers and handle null or negative values
for (int i = 0; i < X.length; i++) {
try {
// Convert the string to an integer
X[i] = Integer.parseInt(inputValues[i]);
// Check if the number is negative or null
if (X[i] == 0 || X[i] < 0) {
X[i] = 1;
}
} catch (NumberFormatException | ArrayIndexOutOfBoundsException e) {
// Handle invalid input or insufficient values
System.out.println("Invalid input. Please enter 10 space-separated numbers.");
return;
}
}
for (int i = 0; i < X.length; i++) {
System.out.println("X[" + i + "] = " + X[i]);
}
}
}