0

Create and populate the two sets of double values, in which the number of elements and the elements themselves are specified by the user. 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.

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. This is my code, but when I run it I get this message :

Exception in thread "main" java.util.NoSuchElementException: No line found
    at java.util.Scanner.nextLine(Scanner.java:1540)
    at SetComparison.main(SetComparison.java:23)

I am new to coding, can anyone tell me what seems to be the problem?

import java.util.*;
import java.lang.*;
import java.io.*;


public class SetComparison
{
    public static void main (String[] args) throws java.lang.Exception
    {
        Scanner scnr = new Scanner(System.in);
        String str = scnr.nextLine();

        String[] allNums = str.split("\\s+");

        int array1Length = Integer.valueOf(allNums[0]);
        int array2Length = Integer.valueOf(allNums[array1Length + 1]);

        double array1[] = new double[array1Length];
        double array2[] = new double[array2Length];

        for (int i = 0; i < array1Length; i++) {
            array1[i] = Double.valueOf(allNums[i + 1]);
        }
        for (int i = 0; i < array2Length; i++) {
            array2[i] = Double.valueOf(allNums[array1Length + i + 2]);
        }

        if (Arrays.equals(array1, array2)) {
            System.out.println("set one and set two are equal");
        } else {
            System.out.println("set one and set two are not equal");
        }
    }
}
azro
  • 35,213
  • 7
  • 25
  • 55

3 Answers3

2

with Scanner you need to check if there is a next line with hasNextLine()

so the loop becomes

while(scnr.hasNextLine()){
    str=scnr.nextLine();
    //...
}

it's readers that return null on EOF

ofcourse in this piece of code this is dependent on whether the input is properly formatted

Afgan
  • 892
  • 5
  • 27
0

You're calling nextLine() and it's throwing an exception when there's no line.

Number945
  • 3,732
  • 4
  • 34
  • 61
0

Try the following modified code:

package com.test;

import java.util.Arrays;
import java.util.Scanner;

public class SetComparison {
    public static void main(String[] args) throws java.lang.Exception {
        Scanner scnr = new Scanner(System.in);
        String str = scnr.nextLine();

        String[] allNums = str.split("\\s+");

        int array1Length = Integer.valueOf(allNums[0]);
        int array2Length = Integer.valueOf(allNums[1]); // Modified

        double array1[] = new double[array1Length];
        double array2[] = new double[array2Length];

        for (int i = 0; i < array1Length; i++) {
            array1[i] = Double.valueOf(allNums[i + 2]); // Modified
        }
        print(array1);
        for (int i = 0; i < array2Length; i++) {
            array2[i] = Double.valueOf(allNums[array1Length + i + 2]);
        }
        print(array2);
        if (Arrays.equals(array1, array2)) {
            System.out.println("set one and set two are equal");
        } else {
            System.out.println("set one and set two are not equal");
        }
    }

    private static void print(double[] array) {
        for (double d : array) {
            System.out.print(d + " ");
        }
        System.out.println();
    }
}

Output:

2 2 3 4 3 4
3.0 4.0 
3.0 4.0 
set one and set two are equal

Output:

2 2 3 4 3 5
3.0 4.0 
3.0 5.0 
set one and set two are not 

I have just checked that your code is also fine:

Output:

2 3 4 2 3 4
3.0 4.0 
3.0 4.0 
set one and set two are equal

2 3 4 2 3 5
3.0 4.0 
3.0 5.0 
set one and set two are not equal

In my code:

  1. 1st element is the length of the first array
  2. 2nd element is the length of the second array
  3. next comes the elements of the first array
  4. next comes the elements of the second array

In your code:

  1. 1st element is the length of the first array
  2. next comes the elements of the first array
  3. next element is the length of the second array
  4. next comes the elements of the second array

Both looks good to me.

Amitabha Roy
  • 727
  • 5
  • 8