Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free.

My question is - how to convert a String ArrayList to an Integer ArrayList?

I have numbers with ° behind them EX: 352°. If I put those into an Integer ArrayList, it won't recognize the numbers. To solve this, I put them into a String ArrayList and then they are recognized.

I want to convert that String Arraylist back to an Integer Arraylist. So how would I achieve that?

This is my code I have so far. I want to convert ArrayString to an Int Arraylist.

        // Read text in txt file.
    Scanner ReadFile = new Scanner(new File("F:\\test.txt"));

    // Creates an arraylist named ArrayString
    ArrayList<String> ArrayString = new ArrayList<String>();

    // This will add the text of the txt file to the arraylist.
    while (ReadFile.hasNextLine()) {

        ArrayString.add(ReadFile.nextLine());
    }

    ReadFile.close();


    // Displays the arraystring.
    System.out.println(ArrayString);

Thanks in advance

Diego

PS: Sorry if I am not completely clear, but English isn't my main language. Also I am pretty new to Java.

share|improve this question

3 Answers 3

up vote 0 down vote accepted

For a more lenient parsing process, you might consider using a regular expression.

Note: The following code is using Java 7 features (try-with-resources and diamond operator) to simplify the code while illustrating good coding practices (closing the Scanner). It also uses common naming convention of variables starting with lower-case, but you may of course use any convention you want).

This code is using an inline string instead of a file for two reasons: It shows that data being processed, and it can run as-is for testing.

public static void main(String[] args) {
    String testdata = "55°\r\n" +
                      "bad line with no number\r\n" +
                      "Two numbers: 123   $78\r\n";
    ArrayList<Integer> arrayInt = new ArrayList<>();
    try (Scanner readFile = new Scanner(testdata)) {
        Pattern digitsPattern = Pattern.compile("(\\d+)");
        while (readFile.hasNextLine()) {
            Matcher m = digitsPattern.matcher(readFile.nextLine());
            while (m.find())
                arrayInt.add(Integer.valueOf(m.group(1)));
        }
    }
    System.out.println(arrayInt);
}

This will print:

[55, 123, 78]
share|improve this answer

You would have to create a new instance of an ArrayList typed with the Integer wrapper class and give it the same size buffer as the String list:

List<Integer> myList = new ArrayList<>(ArrayString.size());

And then iterate through Arraystring assigning the values over from one to the other by using a parsing method in the wrapper class

for (int i = 0; i < ArrayString.size(); i++) {
    myList.add(Integer.parseInt(ArrayString.get(i)));

}
share|improve this answer

You can replace any character you want to ignore (in this case °) using String.replaceAll:

 "somestring°".replaceAll("°",""); // gives "sometring"

Or you could remove the last character using String.substring:

"somestring°".substring(0, "somestring".length() - 1); // gives "somestring"

One of those should work for your case.

Now all that's left is to parse the input on-the-fly using Integer.parseInt:

ArrayList<Integer> arrayInts = new ArrayList<Integer>();

while (ReadFile.hasNextLine()) {
    String input = ReadFile.nextLine();
    try {
        // try and parse a number from the input. Removes trailing `°`
        arrayInts.add(Integer.parseInt(input.replaceAll("°","")));
    } catch (NumberFormatException nfe){
        System.err.println("'" + input + "' is not a number!");
    }
}

You can add your own handling to the case where the input is not an actual number.

share|improve this answer
    
Yes but if i do this it says that it is not a number. Because they have ° behind it so its 273° 250° and it says these are not numbers but i'm trying to go from 273° to int arraylist.. –  Diego Daniels yesterday
    
What do you mean with somestring with what do i need to replace it? Sorry if sound stupid :/ –  Diego Daniels yesterday
    
@DiegoDaniels see edit –  Reut Sharabani yesterday
    
Alright it deleted the ° now but i still get the is not a number message. My Output is now: '' is not a number! [273, 183, 182, 140] –  Diego Daniels yesterday
    
Show an example of the input –  Reut Sharabani yesterday

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.