0

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.

3 Answers 3

2

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.

7
  • 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.. Commented Aug 14, 2015 at 17:22
  • What do you mean with somestring with what do i need to replace it? Sorry if sound stupid :/ Commented Aug 14, 2015 at 17:37
  • 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] Commented Aug 14, 2015 at 17:54
  • Show an example of the input Commented Aug 14, 2015 at 17:59
  • 273° 183° 182° 140° Do you mean this? Commented Aug 14, 2015 at 18:01
1

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]
1

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)));

}

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.