Member Avatar for starsinthesky

how can you convert a string, which is stored in a vector, into an integer?

Recommended Answers

All 5 Replies

Assuming you know the index of the element to be converted do:

int anInt = Integer.parseInt(vector.get(index));

> how can you convert a string, which is stored in a vector, into an
> integer?

Consider searching the archives on the web as a first resort before posting a question. This question has already been answered numerous times. It'll save you a lot of time and will be a step forward in making you an independent/better programmer.

how can you convert a string, which is stored in a vector, into an integer?

the best answer here would be: don't

if it's stored as a String, it may very well contain a numeric value that exceeds the limits of an int
if it's stored as a String, it may also contain non-numeric values

Member Avatar for iamthwee

the best answer here would be: don't

if it's stored as a String, it may very well contain a numeric value that exceeds the limits of an int
if it's stored as a String, it may also contain non-numeric values

And what happens if he NEEDS to, then why not perform some error checking before conversion?

Or of course there is the NumberFormatException which gets thrown if the conversion was unsuccessful, no matter the reason.

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.