Advertisement
Advertisement


String.valueOf(int value) gives error


Question

I am trying to convert an int into a String so that I can put the String values into an SQLite Cursor. I've tried multiple syntax and methods but none seem to work for me.

The Error occurs in MangaItemDB() while trying to convert any Int types aswell as the boolean.

I've looked through several articles like this one but none works for me.

Here's my code:

public class MangaItem {

private int _id;
private String mangaName;
private String mangaLink;
private static String mangaAlpha;
private static int mangaCount;
private static int alphaCount;

private boolean mangaComplete = false;



public MangaItem MangaItemDB(int id, String mangaName, String mangaLink, String mangaAlpha, String mangaCount, String alphaCount, String mangaComplete) {
    MangaItem MangaItemDB = new MangaItem();
    MangaItemDB._id = id;
    MangaItemDB.mangaName = mangaName;
    MangaItemDB.mangaLink = mangaLink;
    MangaItemDB.mangaAlpha = mangaAlpha;
    MangaItemDB.mangaCount = String.valueOf(int mangaCount);
    MangaItemDB.alphaCount = Integer.toString(getAlphaCount());
    MangaItemDB.mangaComplete = String.valueOf(getMangaComplete());
    return MangaItemDB;


}

public void incrementMangaCount() {
    mangaCount++;
}

public int getMangaCount() {
    return mangaCount;
}

public void incrementAlphaCount() {
    alphaCount++;
}

public int getAlphaCount() {
    return alphaCount;
}

public boolean setMangaComplete(boolean mangaComplete) {
    return true;
}

public boolean getMangaComplete() {
    return mangaComplete;
}

/**
 * @return the mangaName
 */
public String getMangaName() {
    return mangaName;
}

/**
 * @param mangaName the mangaName to set
 */
public void setMangaName(String mangaName) {
    this.mangaName = mangaName;
}

/**
 * @return the mangaLink
 */
public String getMangaLink() {
    return mangaLink;
}

/**
 * @param mangaLink the mangaLink to set
 */
public void setMangaLink(String mangaLink) {
    this.mangaLink = mangaLink;
}

/**
 * @return the mangaAlpha
 */
public String getMangaAlpha() {
    return mangaAlpha;
}

/**
 * @param mangaAlpha the mangaAlpha to set
 */
public void setMangaAlpha(String mangaAlpha) {
    this.mangaAlpha = mangaAlpha;
}

/**
 * @return the _id
 */
public int get_id() {
    return _id;
}

/**
 * @param _id the _id to set
 */
public void set_id(int _id) {
    this._id = _id;
}

}

The lines :

    MangaItemDB.mangaCount = String.valueOf(mangaCount);
    MangaItemDB.alphaCount = Integer.toString(getAlphaCount());
    MangaItemDB.mangaComplete = String.valueOf(getMangaComplete());

all give "Type mismatch: cannot convert from String to Int"

2017/05/23
1
2
5/23/2017 12:27:36 PM

Accepted Answer

MangaItemDB.mangaCount = String.valueOf(int mangaCount);

That code shouldn't even compile. You shouldn't pass the parameter type when calling a method, but instead when you call a method you simply pass in the parameter:

MangaItemDB.mangaCount = String.valueOf(mangaCount);

Please post all compilation errors if you have them so we don't have to guess at them.

Edit
You state:

MangaItemDB.mangaCount = String.valueOf(mangaCount);
MangaItemDB.alphaCount = Integer.toString(getAlphaCount());
MangaItemDB.mangaComplete = String.valueOf(getMangaComplete());

all give "Type mismatch: cannot convert from String to Int"

This suggests that you're trying to assign Strings to int variables, and so there appears to be no need to convert the results to a String. Why not simply assign ints to ints and booleans to booleans?

MangaItemDB.mangaCount = mangaCount;
MangaItemDB.alphaCount = getAlphaCount();
MangaItemDB.mangaComplete = getMangaComplete();

Also, why are you using static variables? What is the justification for doing this? There are very specific times when you will want to use static variables, but most often your variables should be instance variables.

5
8/24/2012 5:27:23 PM

MangaItem mangaItemDB = new MangaItem();
MangaItemDB._id = id;
MangaItemDB.mangaName = mangaName;
MangaItemDB.mangaLink = mangaLink;
MangaItemDB.mangaAlpha = mangaAlpha;
MangaItemDB.mangaCount = String.valueOf(int mangaCount);  //change to mangaItemDB .setMangaCount(String.valueOf(mangaCount));
MangaItemDB.alphaCount = Integer.toString(getAlphaCount());
MangaItemDB.mangaComplete = String.valueOf(getMangaComplete());

See above code, Very bad practice. You should use getter and setter methods like :

mangaItemDB.setMangaCount(String.valueOf(mangaCount)); 
2012/08/24

Because MangaCount and Alpha count are static, you do not want to be setting them in the non static context - essentially you are doing nothing. If you want the MangaItem to hold those values, than make a non-static version of MangaCount and AlphaCount.

MangaComplete() returns a boolean, so you aren't going to need to parse it.

Static are variables and methods you want only one kind of for the class, and non-static variables and methods are class specific - they only can be used from the object itself. It seems like set and get MangaCount+AlphaCount should be static.

2012/08/24