Hello All,

I am trying to parse an ArrayList of Strings into an ArrayList of integers. Does anyone have any opinions on how this should be done?

I can parse one element, but parsing the entire string where I am having problems. Any suggestions would be great.

Thanks

Recommended Answers

All 7 Replies

Show an example (of the input and the desired output), as well as the code you have for doing it (at least what you have so far).

Well, I figured out how to parse, however, when i print out the Arrays, it does not print out the first zero.

ex.

08765

it prints 8765

any suggestions on how to fix this?

Member Avatar for iamthwee

Erm maybe because 08765 isn't an integer?

Erm maybe because 08765 isn't an integer?

Exactly. If you really need leading zeros then you have to create that String representation yourself - which will undo the whole point of converting in the first place.

well, I have two arrays of Strings that need to be compared to increment the range of integers up to the integer in the second array. So, I parsed the Strings into Integers in order to compare them. So am I now to parse back to Strings in order to get the initial 0?

Only if they must be stored as strings. If you provided sample input and output and indicated what you need to do with the output, it would be helpful in answering the question.

If it is only a matter of displaying them with leading zeros to the user then there are several formatting methods availble for that (i.e. String. format , Formatter , DecimalFormat )
Example:

int x = 123;
         int y = 23;
         DecimalFormat df = new DecimalFormat("0000");
         System.out.println(df.format(x));
         System.out.println(df.format(y));

Thanks Ezzaral,

I did not even think of that.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.