问题
Hi guys I have this warning on my code. I try to compile using
@SuppressWarnings("unchecked")
It compile but at the time of the execution the program can't find the main class.
How can I solve this problem?
Below the code:
import java.util.*;
@SuppressWarnings("unchecked")
class ProgRub {
public static void main(String argv[]) {
Hashtable rubrica = new Hashtable(20);
String chiave, valore;
Menu mioMenu = new Menu();
int scelta;
scelta = (int) mioMenu.scelta();
while (scelta != 5) {
if (scelta == 1) {
chiave = mioMenu.leggiDato("Nome:");
valoe = mioMenu.leggiDato("Valore:");
rubrica.put(chiave, valore);
} else if (scelta == 2) {
chiave = mioMenu.leggiDato("Nome:");
rubrica.remove(chiave);
} else if (scelta == 3) {
Iterator i = rubrica.keySet().iterator();
while (i.hasNext()) {
chiave = (String) i.next();
valore = (String) rubrica.get(chiave);
System.out.println(chiave + "tel." + valore);
}
}
else if (scelta == 4) {
chiave = mioMenu.leggiDato("Nome:");
if (rubrica.contains(chiave)) {
valore = (String) rubrica.get(chiave);
System.out.println("Telefono:" + valore);
}
else {
System.out.println("Nominativo inesistente.");
}
}
scelta = (int) mioMenu.scelta();
}
System.out.println("Fine programma.");
}
}
回答1:
You are using Hashtable
and Iterator
as raw types. If you use them as Hashtable<String, String>
and Iterator<String>
respectively:
- you shouldn't have to suppress the "unchecked" warnings, and
- you can get rid of a bunch of typecasts.
To understand more, I recommend that you take the time to read up on Java generics:
- https://docs.oracle.com/javase/tutorial/java/generics/index.html
来源:https://stackoverflow.com/questions/54052131/recompile-with-xlint-unchecked-for-details