-1

I am completing a lab assignment and get this error when I compile. The program runs fine, bit would like to fix what is causing the error. The program code and the complete error is below. Thanks as always!

import java.util.HashSet;
import java.util.Scanner;

public class Problema4 {
public static void main(String[] args) {
   Scanner scan = new Scanner(System.in);
   int n,s;
   while(scan.hasNext()){
        n=scan.nextInt();
        s=scan.nextInt();
        int A[] = new int[n];
        for (int i =0; i < n; i++) {
          A[i] =scan.nextInt();
        }
        find(A, s);
    }
}

public static void find(int[] A, int sum) {
    int[] solution = new int[A.length];
    HashSet<String> conjuntos = new HashSet(); // usamos la estructura HashSet para que los subconjuntos no se repitan
    find(A, 0, 0, sum, solution, conjuntos);
    System.out.println(conjuntos.size());
}

public static void find(int[] A, int currSum, int index, int sum, int[] solution, HashSet<String> conjuntos) {
    if (currSum == sum) {
        String subConjunto = "";
        for (int i = 0; i < solution.length; i++) {
            if (solution[i] == 1) {
                subConjunto += "  " + A[i];
            }
        }
        if (!subConjunto.trim().isEmpty()) {
            conjuntos.add(subConjunto);
        }
    }
    if (index == A.length) {
        return;
    } else {
        solution[index] = 1; // selecionamos el elemento
        currSum += A[index];
        find(A, currSum, index + 1, sum, solution, conjuntos);
        currSum -= A[index];
        solution[index] = 0; // no seleccionamos el elemento
        find(A, currSum, index + 1, sum, solution, conjuntos);
    }
    return;
}
}
Santosh Jadi
  • 1,479
  • 6
  • 29
  • 55
  • 2
    replace `new HashSet()` with `new HashSet<>()` – Ricky Mo Oct 24 '18 at 07:08
  • where is your error? – Santosh Jadi Oct 24 '18 at 07:09
  • 1
    A) **there is no error description in your question**. Yes you put a comment in **spanish** into your source code, but sorry: the primary language is English here. So that comment doesnt help. B) Instead, you should always provide a clear [mcve] to enable us to help you. C) it is **not** an error. An error prevents compilation, or causes an issue at runtime. You are dealing with warning D) simply put that warning message into a search engine ... to then find that it is a really common problem. – GhostCat Oct 24 '18 at 07:22
  • See: https://stackoverflow.com/questions/2770321/what-is-a-raw-type-and-why-shouldnt-we-use-it – GhostCat Oct 24 '18 at 07:22

1 Answers1

1

This warning comes up when you are using a Set without type specifier. So new HashSet(); instead of new HashSet<String>(); It shows up because the compiler can't check that you are using the Set in a type-safe way.

If you specify the type of the objects you are storing the warning will go away:

Replace

HashSet<String> conjuntos = new HashSet();

with

HashSet<String> conjuntos = new HashSet<>();

More details can be found here: https://docs.oracle.com/javase/tutorial/java/generics/rawTypes.html

Bas de Groot
  • 676
  • 3
  • 16