import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Scanner; class MyException extends Exception { public MyException(String NegativeArraySizeException) { System.out.println("The integer must be in between 1 and 20"); } } public class arrayTwo { public static void main(String[]args){ Scanner keyboard = new Scanner(System.in); System.out.println("How many integers do you have? (Max 20)"); int[] num = new int[keyboard.nextInt()]; if(num.length<=0 || num.length>20) { System.out.println("You must enter a number in between 1 and 20"); } else for (int i = 0; i < num.length; i++) { System.out.println("Enter element for subscript " + i); num[i] = keyboard.nextInt(); } System.out.println("Here are all of those numbers"); for (int i = 0; i < num.length; i++) { System.out.println(num[i]); } } } Need to figure out the code to get rid of the exception for inputting a negative as an array size.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;
class MyException extends Exception
{
public MyException(String NegativeArraySizeException)
{
System.out.println("The integer must be in between 1 and 20");
}
}
public class arrayTwo {
public static void main(String[]args){
Scanner keyboard = new Scanner(System.in);
System.out.println("How many integers do you have? (Max 20)");
int[] num = new int[keyboard.nextInt()];
if(num.length<=0 || num.length>20) {
System.out.println("You must enter a number in between 1 and 20");
}
else
for (int i = 0; i < num.length; i++) {
System.out.println("Enter element for subscript " + i);
num[i] = keyboard.nextInt();
}
System.out.println("Here are all of those numbers");
for (int i = 0; i < num.length; i++) {
System.out.println(num[i]);
}
}
}
Need to figure out the code to get rid of the exception for inputting a negative as an array size.
Trending now
This is a popular solution!
Step by step
Solved in 3 steps with 1 images