Recompile with -Xlint : unchecked for details

折月煮酒 提交于 2020-08-11 05:23:47

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!