我目前是来自 Python 的 Java 新手,我似乎无法弄清楚如何返回 HashMap,我尝试在网上搜索了几个小时,但找不到任何可以解决我的问题的东西。这是代码。
public HashMap<Integer, Character> dataset(String text) {
for (int i = 0; i < text.length(); i++) {
char value = text.charAt(i);
HashMap<Integer, Character> dict = new HashMap<Integer, Character>();
dict.put(i, value);
System.out.println(dict.getClass().getSimpleName());
return dict; // Here's the error
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter text: ");
String text = sc.next();
System.out.println("============================");
System.out.println("1. Convert to uppercase");
System.out.println("2. Conver to lowercase");
System.out.println("3. Convert to a dataset");
System.out.print("Enter selection: ");
String choice = sc.next();
StringConverter obj = new StringConverter();
switch(choice) {
case "1":
text = obj.upperCase(text);
System.out.println("=====Converted to uppercase=====");
System.out.println(text);
break;
case "2":
text = obj.lowerCase(text);
System.out.println("=====Converted to lowercase=====");
System.out.println(text);
break;
case "3":
obj.dataset(text);
break;
}
}
我什至仔细检查了我返回的变量是否是 HashMap。
这是一个例外。
线程“main”java.lang.Error 中的异常:未解决的编译问题:此方法必须返回 HashMap<Integer,Character> 类型的结果
我知道这可能是一个新手错误,但经过数小时的研究,我找不到任何有用的东西。