无法在基本类型int上调用toString() [英] Cannot invoke toString() on the primitive type int

查看:4340
本文介绍了无法在基本类型int上调用toString()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上,我正在尝试做的是获取商品ID,并从ini设置价格,基本上就像:itemid:price
但是,我不能简单地做item.getId()。toString ()。
我正在尝试获取项目
我该怎么做才能使它成为一个字符串?

Basically, what I'm trying to do, is get the item ID, and set a price from a ini, basically like: itemid:price but, i cannot simply do item.getId().toString(). I'm trying to get item What can I do to make it a string?

public static void getBuyPrice(Item item) {
    try {
        String itemId = item.getId().toString();
        BufferedReader br = new BufferedReader(new FileReader(new File(
                "./data/prices.ini")));
        String line;
        while ((line = br.readLine()) != null) {
            if (line.equals(itemId)) {
                String[] split = line.split(":");
                item.getDefinitions().setValue(Integer.parseInt(split[1]));
            }
        }
        br.close();
    } catch (Throwable e) {
        System.err.println(e);
    }
}

这是我的代码,(当然我有item.getId()。toString())中的错误,我该怎么做才能将其转换为字符串?

That is my code, (of course I have the error at item.getId().toString()), What can I do to convert that to a string?

推荐答案

原语类型没有方法,因为它们不是Java中的对象。你应该使用匹配的类:

Primitive types do not have methods, as they are not objects in Java. You should use the matching class:

Integer.toString(item.getId());

这篇关于无法在基本类型int上调用toString()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆