1

I have an arraylist, say arr. Now this arraylist stores numbers as strings. now i want to convert this arraylist to integer type. So how can i do that???

ArrayList arr = new ArrayList();

String a="Mode set - In Service", b="Mode set - Out of Service";

if(line.contains(a) || line.contains(b)) {
    StringTokenizer st = new StringTokenizer(line, ":Mode set - Out of Service In Service");
    while(st.hasMoreTokens()) {
      arr.add(st.nextToken());  
    }
}
3
  • 5
    Your code and your question seem to not fit together. Commented Jul 1, 2011 at 13:31
  • cannot compute :/... i don't see an array of string numbers? Commented Jul 1, 2011 at 13:32
  • @nfechner: At the top of my code I have declared an array. Now in "IF", my file contains certain part i.e. i have defined already as String a or b, it tokenize the line and add it to arraylist....arr.add(st.nextToken());...... Now the resulting array list is in string type and i want to convert this list in integer. Anyways, thanks for replying guys. Commented Jul 5, 2011 at 12:06

6 Answers 6

5

Since you're using an untyped List arr, you'll need to cast to String before performing parseInt:

List<Integer> arrayOfInts = new ArrayList<Integer>();
for (Object str : arr) {
   arrayOfInts.add(Integer.parseInt((String)str));
}

I recommend that you define arr as follows:

List<String> arr = new ArrayList<String>();

That makes the cast in the conversion unnecessary.

Sign up to request clarification or add additional context in comments.

Comments

2

run the below code,i hope it meets you requirement.

import java.util.*;
import java.lang.*;
import java.io.*;


class Ideone
{
    public static void main (String[] args) throws java.lang.Exception
    {
        ArrayList<String> strArrayList= new ArrayList<String>();
            strArrayList.add("1");
            strArrayList.add("11");
            strArrayList.add("111");
            strArrayList.add("12343");
            strArrayList.add("18475");
        int[] ArrayRes = new int[strArrayList.size()];

        int i = 0;
        int x = 0;
        for (String s : strArrayList)
        {

            ArrayRes[i] = Integer.parseInt(s);
            System.out.println(ArrayRes[i]);
            i++;
        }

    }
}

Output:

1
11
111
12343
18475

Comments

1

To convert to an integer array, you will input as a string array then go through each one and change it to an int.

    public int[] convertStringArraytoIntArray(String[] sarray) throws Exception {
if (sarray != null) {
//new int for each string
int intarray[] = new int[sarray.length];
//for each int blah blah to array length i
for (int i = 0; i < sarray.length; i++) {
intarray[i] = Integer.parseInt(sarray[i]);
}
return intarray;
}
return null;
}

Comments

1
final List<String> strs = new ArrayList();

strs.add("1");
strs.add("2");

Integer[] ints = new Integer[strs.size()];

for (int i = 0; i<strs.size(); i++){
    ints[i] = Integer.parseInt(strs.get(i));
}

Comments

0

use the Integer.parseInt() method.

http://www.java2s.com/Code/Java/Language-Basics/Convertstringtoint.htm

Comments

0

If you know that you have an arraylist of string but in your you wil use the same list as list of integer so better while initializing array list specify that the array list must insert only int type of data

instead of writing ArrayList arr = new ArrayList(); you could have written ArrayList<Integer> arr = new ArrayList<Integer>();

Alternate solution

If you want to convert that list into Integer ArrayList then use following code

How to convert String ArrayList into ArrayList of int

ArrayList<String> oldList = new ArrayList<String>();
        oldList.add(""+5);
        oldList.add(""+5);    
ArrayList<Integer> newList = new ArrayList<Integer>(oldList.size());
            for (String myInt : oldList) { 

              newList.add(Integer.parseInt(myInt)); 
            }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.