File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes toString or String.valueOf() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "toString or String.valueOf()" Watch "toString or String.valueOf()" New topic
Author

toString or String.valueOf()

Kingsley Mullers
Ranch Hand

Joined: Jan 14, 2005
Posts: 48
I have declared the following values as int and double but my problem is that I�ve created an array which returns a string which means since Prefnum is declared as an int then it will have to be converted to a string, now do I used String.valueOf or toString to convert so I can print out these values as strings? The error that i'm getting when using either one of the two is incompatible data type.

thanks


int Prefnum=0;
int Arefnum=0;
int Pserial=0;
double Pvalue=0;

try {

String[] proceedingsList=crime.getProceedingsDetails(conn,referenceNumber);
// Prefnum=String.valueOf(proceedingsList[0]);
// Prefnum=(proceedingsList[0]).toString();

Prefnum=proceedingsList[0]);
if(proceedingsList[0]==null){
Prefnum="";
}else{
Prefnum=(proceedingsList[0]);
}

Ptype=proceedingsList[1];
if(proceedingsList[1]==null){
Ptype="";
}else{
Ptype=proceedingsList[1];
}
Pserial=proceedingsList[2];
if(proceedingsList[2]==null){
Pserial="";
}else{
Pserial=proceedingsList[2];
}
Ton Fever
Greenhorn

Joined: Aug 29, 2005
Posts: 2
Can you tell me what the error you got is?
Kingsley Mullers
Ranch Hand

Joined: Jan 14, 2005
Posts: 48
i'm getting "incompatible type; found: java.lang.String,required: int" error.

thanks
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42933
    
  68
Either "String.valueOf(Prefnum)" or "(new Integer(Prefnum)).toString()" should work.
Kingsley Mullers
Ranch Hand

Joined: Jan 14, 2005
Posts: 48
i've tried it like this...

String Prefnum;

Prefnum=Integer.parseInt(proceedingsList[0]).toString(); //incompatible type

and also tried

int Prefnum=0;
Prefnum=String.valueOf(proceedingsList[0]); //incompatible type

what am i missing here?

thanks
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42933
    
  68
1) Integer.parseInt takes a String as parameter, not an int, and it returns an int, which is not an object, and thus does not have a toString method.

2) String.valueOf returns a String, not an int.

Try
int Prefnum = Integer.parseInt(proceedingsList[0]);

If you're in doubt what kind of parameter a method accepts, or what kind of value it returns, you can check the javadocs for java.lang.Integer and java.lang.String.
[ August 29, 2005: Message edited by: Ulf Dittmer ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: toString or String.valueOf()