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]); } } } attempted to use this code to fix the exception when a negative number is entered by the user, but it still gives back the exception. I would like to know how to amend it or how to restart it, if that's what it takes.
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]);
}
}
}
attempted to use this code to fix the exception when a negative number is entered by the user, but it still gives back the exception. I would like to know how to amend it or how to restart it, if that's what it takes.
Trending now
This is a popular solution!
Step by step
Solved in 2 steps