Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Putting digits one by one  RSS feed

 
Oleg Korsakov
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
... I have to put digits as a result of a % operation into string
to accumulate there in cyrcle by putting one digit by another and using concatenation
declare: String mstr = result % n; //result has int type
then in cycle
mstr += result % n; // symbol by symbol

n = 2 (5,...)

Which casting I have to do in the cycle and in the declaration ?
ps
If I write: String mstr = (String)result % 2; //-- incompatible types

My thanks for any help
 
Henry Wong
author
Sheriff
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Oleg Korsakov:

Which casting I have to do in the cycle and in the declaration ?
ps
If I write: String mstr = (String)result % 2; //-- incompatible types


For the same reason why you can't directly cast a string to an int, you also can't cast an int to a string. To convert a int to a string, you can use the Integer class. Something like...

String mstr = Integer.toString(result % 2);

Henry
 
Oleg Korsakov
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!