Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot invoke toString() on the primitive type int

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);
    }
}

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

like image 941
Alex DaSilva Avatar asked Apr 01 '12 04:04

Alex DaSilva


2 Answers

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

Integer.toString(item.getId());
like image 194
MByD Avatar answered Sep 24 '22 04:09

MByD


String itemId = Integer.toString(item.getId());
like image 36
Skip Head Avatar answered Sep 23 '22 04:09

Skip Head