Complete the program below in order to make run properly package lab3ex1witherro
ID: 3748332 • Letter: C
Question
Complete the program below in order to make run properly
package lab3ex1witherror; import java.io.*;
import java.util.*;
public class Lab3Ex1witherror {
public static void main(String[] args) {// do not change anything on this line Scanner read = new Scanner(System.in);
String languages[] = {"Python", "Java", "Java Script", "Perl", "C", "C++"}; int a[] = {5, 0};
Scanner input = null; int number;
int i, j;
// Exception 1
for (int i = 0; i <= languages.length; i++) {
System.out.println(languages[i]); }
//Exception 2
System.out.println("enter two integr value for i and j"); i = read.nextInt();
j = read.nextInt();
System.out.println("The result is :" + a[i] / a[j]);
// Eception3
input = new Scanner(new FileReader("data.in")); while (input.hasNext()) {
number = input.nextInt();
System.out.println(number); }
input.close();
// Eception4 Change 100 to 1000000000
} }
long data[] = new long[1000000000];
Explanation / Answer
Please find the modified code below.
CODE
====================
package lab3ex1witherror;
import java.io.*;
import java.util.*;
public class Lab3Ex1witherror {
public static void main(String[] args) {// do not change anything on this line Scanner read = new Scanner(System.in);
String languages[] = {"Python", "Java", "Java Script", "Perl", "C", "C++"};
int a[] = {5, 0};
Scanner input = null;
Scanner read = new Scanner(System.in);
int number;
int i, j;
try{
// Exception 1: In the for loop, the value of i runs till languages.length, but the last index of array languages is languages.length-1,
// hence, when i = languages.length, it will throw an ArrayIndexOutOfBoundsException exception.
for (i = 0; i <= languages.length; i++) {
System.out.println(languages[i]);
}
} catch (ArrayIndexOutOfBoundsException e) {
e.printStackTrace();
}
//Exception 2: In this case if the user enters 1 for j, then a[j] = 0. As a result, a[i]/0 will give Divide by zero error.
try {
System.out.println("enter two integr value for i and j"); i = read.nextInt();
j = read.nextInt();
System.out.println("The result is :" + a[i] / a[j]);
} catch (ArithmeticException e) {
System.out.println("The denominator cannot be 0!!");
}
// Eception3 : In this case, if the file "data.in" is not found, it will throw an FileNotFoundException.
try {
input = new Scanner(new FileReader("data.in"));
} catch (FileNotFoundException e) {
e.printStackTrace();
} while (input.hasNext()) {
number = input.nextInt();
System.out.println(number);
}
input.close();
read.close();
// Eception4 Change 100 to 1000000000
}
}
NOTE: I could not find relevant code for Exception 4, hence i have skipped it. If you need further assisstance, kindly comment further.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.