I'm fairly new to Java but I'm working on an assignment and I'm a bit lost. An array is declared and its elements can only be numbers greater than 0 and less than 100. If I run the code I have, it asks me to enter the array size and then the array values, but it accepts values 100 and above.
If input is less than zero or 100 and greater, how can I return something like "Please enter values only >0 and <100?
package arrays;
import java.util.Scanner;
import java.util.stream.IntStream;
public class ArrayAssignment {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner s = new Scanner(System.in);
System.out.println("Enter array size: ");
int size = s.nextInt();
int[] Array = IntStream.rangeClosed(1, 99).toArray();
System.out.println("Enter array values: ");
for (int i = 0; i < size; i ) {
int value = s.nextInt();
Array[i] = value;
}
int length = Array.length;
int sum = 0;
for (int i = 0; i < Array.length; i ) {
sum = Array[i];
}
double average = sum / length;
System.out.println("Average of array: " average);
}
}
Running code: Enter array size: 10
Enter array values:
12
34
12
1
2
4
1
1100
10
14
Average of array: 61.0
CodePudding user response:
Keep checking the input in an infinite loop until the user inputs valid input.
package arrays;
import java.util.Scanner;
import java.util.stream.IntStream;
public class ArrayAssignment {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner s = new Scanner(System.in);
while (true) {
System.out.println("Enter array size: ");
int size = s.nextInt();
if (size < 100 && size > 0) {
break;
} else {
System.out.println("Please enter values only >0 and <100");
}
}
int[] Array = IntStream.rangeClosed(1, 99).toArray();
System.out.println("Enter array values: ");
for (int i = 0; i < size; i ) {
int value = s.nextInt();
Array[i] = value;
}
int length = Array.length;
int sum = 0;
for (int i = 0; i < Array.length; i ) {
sum = Array[i];
}
double average = sum / length;
System.out.println("Average of array: " average);
}
}
CodePudding user response:
You can allow only specific inputs by modifying your for
loop to not include the i
in the definition, but rather only increase the counter if a valid value is given.
This loop will now look like this:
for (int i = 0; i < size;) {
System.out.println("Enter array values: ");
int value = s.nextInt();
if (value > 100 || value <= 0) {
System.out.println("Please enter values greater than 0 and less than or equal to 100.");
}
else {
Array[i] = value;
i ;
}
}
Note that you can also just change this to do-while
loop or a while
loop and get the same results (those are probably better built for this kind of situation). Additionally, this will still break when a char
or String
is inputted, as I did not add handling for those situations.
Extra Notes:
Additionally I noticed you are using double average = sum / length;
which would perform integer division so you would never have a decimal result. You can fix this by casting sum
to double
before dividing using the following:
double average = (double) sum / length;
A final thing to look at is your int[] array = IntStream.rangeClosed(1, 99).toArray();
line. I am not sure if you meant to do int[] array = IntStream.rangeClosed(1, size).toArray();
instead and create an array based on the size that the user inputted but you may also need to change this.