0

I want to convert a int to String in java but I can't :

this is the code I used :

 jTextField1.setText((String)l.getCode());

and this is the error I got :

Inconvertible types
required:java.lang.String
found:   int
Renaud is Not Bill Gates
  • 1,684
  • 34
  • 105
  • 191

8 Answers8

9

You can not typecast int to string try below code.

jTextField1.setText(String.valueOf(l.getCode()));
Rais Alam
  • 6,970
  • 12
  • 53
  • 84
3

Use

   Integer.toString(l.getCode);

http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/Integer.html#toString%28int%29

cowls
  • 24,013
  • 8
  • 48
  • 78
2

Try something like this:

Integer.valueOf(l.getCode()).toString()

You cannot convert int simple type to String object.

user
  • 3,058
  • 23
  • 45
  • [`String.valueOf(int)`](http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#valueOf(int)) or [`Integer.toString(int)`](http://docs.oracle.com/javase/7/docs/api/java/lang/Integer.html#toString%28int%29) is simpler. – Mark Rotteveel Mar 05 '13 at 11:38
2

maybe you could try:

String.valueOf(int)
Kent
  • 189,393
  • 32
  • 233
  • 301
2

This should be what you are looking for:

jTextField1.setText(String.valueOf(l.getCode()))
araknoid
  • 3,065
  • 5
  • 33
  • 35
2
jTextField1.setText(String.valueOf(l.getCode()));
Xavier DSouza
  • 2,861
  • 7
  • 29
  • 40
2

There are three ways you convert an integer to string firstly using the built-in converter,

jTextField1.setText("" + l.getCode())

Secondly you could use the static method toString(int) of the Integer class,

jTextField1.setText(Integer.toString(l.getCode()))

You could also use a formatter but this in not recommended as it just makes the code cumbersome and difficult to understand.

jTextField1.setText(String.format("%d", l.getCode()))
tomldac
  • 44
  • 2
  • RE "" + int: http://stackoverflow.com/questions/4105331/how-to-convert-from-int-to-string – ldam Mar 05 '13 at 12:31
1

Try this:

jTextField1.setText(Integer.toString(l.getCode()));
niculare
  • 3,629
  • 1
  • 25
  • 39