[英]Java - Type mismatch: cannot convert from element type Object to String
[英]Type mismatch: cannot convert from element type Object to String error
所以我在下面的代码中得到了一个错误:
for (final String key : keySet)
我不知道该怎么办,请帮忙。 :C 如果你回答会很棒
Config.log("Loading " + fileName);
final Properties props = new Properties();
props.load(in);
final Set keySet = props.keySet();
// error here!
for (final String key : keySet) {
final String prefix = "width.";
if (key.startsWith(prefix)) {
final String numStr = key.substring(prefix.length());
final int num = Config.parseInt(numStr, -1);
if (num < 0 || num >= FontRenderer.charWidth.length) {
continue;
}
final String value = props.getProperty(key);
final float width = Config.parseFloat(value, -1.0f);
if (width < 0.0f) {
continue;
}
FontRenderer.charWidth[num] = width;
props.keySet() 返回一组对象,您的 for 循环认为它正在处理 String 类型的键。
您需要手动将键转换为字符串或使用 toString():
Config.log("Loading " + fileName);
final Properties props = new Properties();
props.load(in);
final Set keySet = props.keySet();
for (final Object key : keySet) {
final String prefix = "width.";
if (key.toString().startsWith(prefix)) {
final String numStr = key.toString().substring(prefix.length());
final int num = Config.parseInt(numStr, -1);
if (num < 0 || num >= FontRenderer.charWidth.length) {
continue;
}
final String value = props.getProperty(key.toString());
final float width = Config.parseFloat(value, -1.0f);
if (width < 0.0f) {
continue;
}
FontRenderer.charWidth[num] = width;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.