• Register
1 vote
41 views

Problem:

I am facing a problem to convert my string type array to an integer type array. Let’s say I have an array of string

String [] mystr = {"123", "234", "345", "456"};

and I want to cast my string to an integer type

String [] mystr = {123, 234, 345, 456};

I don’t know what do you say, or what is the proper term! I gave an example to describe it properly. So, let me put my question this way how Java converts string array to int array? Please, shed some light on this.
Thanks in advance.

12 7 7
15,250 points

Please log in or register to answer this question.

1 Answer

1 vote

Solution:

You can easily convert or cast your string array data into an integer array data. There is a method called parseInt() available in java to convert your string array to integer array. This method converts each string element to an integer element to the length of an array. You just need to populate it with the integer array in your program. 

import java.util.Arrays;
public class MyClass {
   public static void main(String args[]) {
      String [] mystr = {"123", "234", "345", "456"};
      int size = mystr.length;
      int [] myarr = new int [size];
      for(int i=0; i<size; i++) {
         myarr[i] = Integer.parseInt(mystr[i]);
      }
      System.out.println(Arrays.toString(myarr));
   }
}

This program should print an output

[123, 234, 345, 456]

 

13 9 6
94,240 points

Related questions

0 votes
1 answer 3 views
3 views
Problem: In Java 8 &middot; Convert the specified primitive array to a sequential Stream using Arrays. stream() &middot; Box each element of the Stream to an Integer using IntStream. Get the set of integers. Convert Set of Integer to Stream of Integer. This is done using Set. stream(). Convert Stream of Integer to IntStream. Convert IntStream to int[].
asked 1 day ago Ali 4.4k points
0 votes
1 answer 13 views
13 views
Problem: How do you convert an stringa intin Java?I have a string that contains only numbers (numbers 1-4 to be specific), and I want to return the number it represents (actually I have a StringBufferno String). For example, given the string "1234"the result must be the number 1234.
asked Feb 12 sasha 8.7k points
0 votes
1 answer 13 views
13 views
Problem: How can I convert a String to an int in Java? My String contains only numbers, and I want to return the number it represents. For example, given the string "1234" the result should be the number 1234.
asked Feb 3 Ali 4.4k points
0 votes
1 answer 9 views
9 views asked Jan 26 ahmedadel9090 2.6k points
0 votes
1 answer 5 views
5 views
Problem
asked Jan 13 alexh 2.5k points
0 votes
1 answer 8 views
8 views
Problem: How to Get the list of String. How to Convert List of String to List of Integer using Lists. transform(). This is done using passing Integer. parseInt() method as lambda expression for transformation. How to Return/Print the list of String.
asked Nov 5, 2020 Mashhoodch 9.1k points
0 votes
1 answer 22 views
22 views
I have a byte array filled with hex numbers and printing it the easy way is pretty pointless because there are many unprintable elements.
asked Feb 14 TeamScript 13.5k points
0 votes
1 answer 2 views
2 views
Problem: I want to convert string array to int array using C#. Is there any way to complete the task?
asked 2 days ago Umar Ahmad 2.1k points
1 vote
1 answer 37 views
37 views
Problem: I am beginner in pyhton prgramming language, I tried to convert string array to int array using python so I wrote a code which contains- testing_list = ['4', '2', '3', '9', '5'] And my code is: testing_list = ['4', '2', '3', '9', '5 ... list is : str(testing_list) Converted list is : str(testing_list) why am I not getting the converted result and what is the way to get the acurate result?
asked Mar 29, 2020 LizzyM 6.1k points
0 votes
1 answer 15 views
15 views
I'm sure there's a better way to do this.
asked Jan 24 TeamScript 13.5k points