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

Method to convert String[] to int[]  RSS feed

 
Sandeep Swaminathan
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Very basic question:

I'm trying to get input from the user in an array (in a method) using 'for' loop construct



sample[count] & sampleStr[count] are instance variables that I declare in the beginning



1. My doubt is can we use BufferedReader to get input in an array or is there any other efficient way?
2. How to convert string[] to int[]?
 
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
I don't think there is a convenience method for this in the core API. You'll have to instantiate the int array, and parse each element of the string array to the int array.

Henry
 
Sandeep Swaminathan
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok. So you meant to say something like this right?

 
Rob Spoor
Sheriff
Posts: 21087
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's the way I would have done it, although I might have named the method "toIntArray"; the StringArray part can be deducted from the parameter list.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!