Need help editing my code i keep getting the same error message import java.util
ID: 3820886 • Letter: N
Question
Need help editing my code i keep getting the same error message
import java.util.*;
import java.lang.*;
import java.io.*;
public class SetComparison
{
public static void main(String[] args)
{
int count = 0; //variable to manipulate while comparing arrays
Scanner con = new Scanner(System.in);
int array1size = 0;
int array2size = 0;
System.out.println("Enter the size of Array 1");
array1size = con.nextInt();
double[] arr1 =new double[array1size];
System.out.println("Enter the size of Array 2");
array2size = con.nextInt();
double[] arr2 =new double[array2size];
if (array1size == array2size)
{
System.out.println("Enter the values of Array 1");
for (int i=0;i<array1size;i++)
arr1[i]=con.nextDouble();
System.out.println("Enter the values of Array 2");
for (int i=0;i<array2size;i++)
arr2[i]=con.nextDouble();
for (int i=0;i<array2size;i++)
{
if((Math.abs(arr1[i]-arr2[i])>0.001)){
count++;
System.out.println("The values of arr1[" + i + "] and arr2[" + i + "] are not equal");
break;
}
}
if(count == 0){
System.out.println("set one and two are equal");
}else{
System.out.println("set one and two are not equal");
}
}
else
System.out.println("The Array sizes are not same ");
}
}
Explanation / Answer
import java.util.*;
public class SetComparison {
public static void main(String[] args) {
int count = 0; // variable to manipulate while comparing arrays
Scanner con = new Scanner(System.in);
int array1size = 0;
int array2size = 0;
array1size = con.nextInt();
double[] arr1 = new double[array1size];
for (int i = 0; i < array1size; i++)
arr1[i] = con.nextDouble();
array2size = con.nextInt();
double[] arr2 = new double[array2size];
for (int i = 0; i < array2size; i++)
arr2[i] = con.nextDouble();
if (array1size == array2size) {
for (int i = 0; i < array2size; i++) {
if ((Math.abs(arr1[i] - arr2[i]) > 0.001)) {
count++;
break;
}
}
if (count == 0) {
System.out.println("set one and two are equal");
} else {
System.out.println("set one and two are not equal");
}
} else
System.out.println("The Array sizes are not same ");
con.close();
}
}
// Sample run
6
45.24 54.67 42.55 44.61 65.29 49.75
6
45.24 54.67 41.99 44.61 65.29 49.75
set one and two are not equal
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.