-1

Hi, I would really appreciate the help for this lab. I don't understand what I did wrong; for example, I don't understand the program errors displayed.

5.13 Lab 5b

Objectives

In this lab you will practice using for loops with arrays and comparing double values, using a threshold, to see if two sets of numbers are the same.

Background Reading

ZyBooks Chapter 5

Task

The first comment of your main method should be your identification comment:

//Lab5b, [Name], [mascID]

You will need to then:

Create and populate the two sets of double values, in which the number of elements and the elements themselves are specified by the user. (i.e. the user will state how many numbers are in the first set, give the values for that set and then move on to the second set of numbers).

Iterate through the sets and compare the values. For the purposes of this lab, we will say that the order matters in determining if the sets are the same. (HINT: This also means that the lengths of the arrays must be the same).

If the arrays are the same in both length and values, print set one and set two are equal. Otherwise, print set one and set two are not equal

Comparing double values using a threshold

Recall that floating-point numbers may differ in a very small value. For example, 1.0 might actually be stored as 0.9999999... or 1.0000000002.

When comparing these values, we often use a "tolerance" level that says, "if the difference is smaller than some amount, then it is negligible and the numbers can be thought of as the same." We can achieve this by comparing the absolute value of the difference of the elements to the tolerance. For example,

if ( Math.abs( array1[0] - array2[0] ) > 0.001 ) )
    System.out.println("The values of array1[0] and array2[0] are not equal");

Code:

import java.lang.Math;
import java.util.Scanner;

public class SetComparison {
   public static void main (String [] args) {
      
      Scanner scnr = new Scanner(System.in);
      
      int array1Elems = 0;
      int array2Elems = 0;
      int i = 0;
      int j = 0;
      
      array1Elems = scnr.nextInt();
      array2Elems = scnr.nextInt();
      
      double array1[] = new double[array1Elems];
      double array2[] = new double[array2Elems];
      
      for (i = 0; i < array1Elems; ++i) {
         array1[i] = scnr.nextInt;
         for (j = 0; j < array2Elems; ++j) {
            array2[j] = scnr.nextInt;
         }
         if (Math.abs(array1[i] - array2[j]) > 0.001) {
            System.out.println("set one and set two are not equal");
         }
         else if (Math.abs(array1[i] - array2[j]) < 0.001) {
            System.out.println("set one and set two are equal");
         }
      }
   }
}

Enter program input

6 45.24 54.67 42.55 44.61 65.29 49.75 6 45.24 54.67 42.55 44.61 65.29 49.75

Program errors displayed here SetComparison.java:23: error: cannot find symbol array1[i] = scnr.nextInt; ^ symbol: variable nextInt location: variable scnr of type Scanner SetComparison.java:25: error: cannot find symbol array2[j] = scnr.nextInt; ^ symbol: variable nextInt location: variable scnr of type Scanner 2 errors

Tazbirul Haque
  • 233
  • 1
  • 3
  • 15
Muhammad
  • 1
  • 1
  • does your code compile? your using `double` to store an `int` – Minh Kieu Nov 15 '17 at 08:00
  • I updated the code and run the code snippet. No more errors but no outputs. – Minh Kieu Nov 15 '17 at 08:02
  • No, it does not, and I changed the double's to int's and it still does not compile. I need to use double's because that's what the lab calls for. – Muhammad Nov 15 '17 at 08:02
  • Yeah, I'm also confused why there are no outputs. – Muhammad Nov 15 '17 at 08:03
  • You could try `array1[i] = scnr.nextDouble;` - the fact that it compiled shows you got over the issue. Try debug it next. I think no output because of no input. – Minh Kieu Nov 15 '17 at 08:05
  • `scnr.nextInt()`. You need the parentheses. But then you've got to deal with the `ArrayIndexOutOfBoundsException` that will occur after. – Andy Turner Nov 15 '17 at 08:19

2 Answers2

0

Besides what @Andy Turner stated in the comments (you are missing the ()), your problem is in the nested loops*. You need to break out of the inner loop after you check the values.

Note: This will also print whether they are equal or not equal on each iteration, and not overall.

for (i = 0; i < array1Elems; i++) {
        array1[i] = scnr.nextDouble();
        for (j = 0; j < array2Elems; j++) {
            array2[j] = scnr.nextDouble();
            if (Math.abs(array1[i] - array2[j]) > 0.001) {
                System.out.println("set one and set two are not equal");
            } else if (Math.abs(array1[i] - array2[j]) < 0.001) {
                System.out.println("set one and set two are equal");
            }
            break;
        }
    }

*Whilst there are far better ways to do this, this is just to match the OPs code as close as possible

achAmháin
  • 4,176
  • 4
  • 17
  • 40
  • Thanks but now it says: SetComparison.java:23: error: incompatible types: possible lossy conversion from double to int array1[i] = scnr.nextDouble(); ^ SetComparison.java:25: error: incompatible types: possible lossy conversion from double to int array2[j] = scnr.nextDouble(); ^ 2 errors – Muhammad Nov 15 '17 at 08:43
  • Mine doesn't? See full code: https://ideone.com/avgadu – achAmháin Nov 15 '17 at 08:45
  • What it says for me, using your version of the full code, is this error: Exception in thread "main" java.util.InputMismatchException at java.util.Scanner.throwFor(Scanner.java:864) at java.util.Scanner.next(Scanner.java:1485) at java.util.Scanner.nextInt(Scanner.java:2117) at java.util.Scanner.nextInt(Scanner.java:2076) at SetComparison.main(SetComparison.java:17) – Muhammad Nov 15 '17 at 09:00
  • That would suggest your input is wrong. You need to input how many numbers you want in `array1` and hit return. Then enter how many numbers you want in `array2` and hit return. Then enter the first value in `array1` and hit return. Then enter the first value in `array2` and hit return and so on and so forth. If you want it to work with one big input like in the yellow box above, the whole program will need to be changed around. Note that an error will occur if you don't specify the same amount of numbers in each `array`. – achAmháin Nov 15 '17 at 09:14
  • My output was this but it still didn't work. 6 45.24 54.67 42.55 44.61 65.29 49.75 6 45.24 54.67 42.55 44.61 65.29 49.75 – Muhammad Nov 15 '17 at 09:29
  • @Muhammad I'm not sure you fully understood what I was saying in my last comment. Either way, see https://ideone.com/j8xrQQ if you want to literally input `6 45.24 54.67 42.55 44.61 65.29 49.75 6 45.24 54.67 42.55 44.61 65.29 49.75` on one line as your input. – achAmháin Nov 15 '17 at 09:49
0

Try this answer I think this will help you...

import java.lang.Math;
import java.util.Scanner;

public class SetComparison {
   public static void main (String [] args) {      
      Scanner scnr = new Scanner(System.in);

      int array1Elems = 0;
      int array2Elems = 0;
      int i = 0;
      int j = 0;

      array1Elems = scnr.nextInt();
      array2Elems = scnr.nextInt();

      double array1[] = new double[array1Elems];
      double array2[] = new double[array2Elems];

      for (i = 0; i < array1Elems; ++i) {
         array1[i] = scnr.nextInt();
      }
      for (j = 0; j < array2Elems; ++j) {
         array2[j] = scnr.nextInt();
      }   
      for (i = 0; i < array1Elems; ++i) {
         for (j = 0; j < array2Elems; ++j) {
            if (Math.abs(array1[i] - array2[j]) > 0.001) {
               System.out.println("set one and set two are not equal");
            } else if (Math.abs(array1[i] - array2[j]) < 0.001) {
               System.out.println("set one and set two are equal");
            }
         }         
      }
   }
}
Sumit Mali
  • 11
  • 3