![](/img/trans.png)
[英]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.