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.
![](/static/compass_v2/shared-icons/check-mark.png)
Trending now
This is a popular solution!
Step by step
Solved in 2 steps
![Blurred answer](/static/compass_v2/solution-images/blurred-answer.jpg)
![Database System Concepts](https://www.bartleby.com/isbn_cover_images/9780078022159/9780078022159_smallCoverImage.jpg)
![Starting Out with Python (4th Edition)](https://www.bartleby.com/isbn_cover_images/9780134444321/9780134444321_smallCoverImage.gif)
![Digital Fundamentals (11th Edition)](https://www.bartleby.com/isbn_cover_images/9780132737968/9780132737968_smallCoverImage.gif)
![Database System Concepts](https://www.bartleby.com/isbn_cover_images/9780078022159/9780078022159_smallCoverImage.jpg)
![Starting Out with Python (4th Edition)](https://www.bartleby.com/isbn_cover_images/9780134444321/9780134444321_smallCoverImage.gif)
![Digital Fundamentals (11th Edition)](https://www.bartleby.com/isbn_cover_images/9780132737968/9780132737968_smallCoverImage.gif)
![C How to Program (8th Edition)](https://www.bartleby.com/isbn_cover_images/9780133976892/9780133976892_smallCoverImage.gif)
![Database Systems: Design, Implementation, & Manag…](https://www.bartleby.com/isbn_cover_images/9781337627900/9781337627900_smallCoverImage.gif)
![Programmable Logic Controllers](https://www.bartleby.com/isbn_cover_images/9780073373843/9780073373843_smallCoverImage.gif)