AlmostDone AlmostDone -4 years ago 42
Java Question

Java - Store last characters in array

Is it possible to store the last characters from a string in an array as a double?

I have following string:

String s="random number 230";

That means I would like to have the array to look like this:

double array[] = {230}

Answer Source

I'm not sure what you are looking for, but based entirely on the example you gave, you could use this:

String[] words = s.split(" ");
String numberString = words[words.length-1];

    double[] array = new double[]{Double.parseDouble(numberString)};
catch(NumberFormatException e){



.split turns a string into an array of strings based on some delimiter.

I used a blank space (i.e. " ").

Then, I reference the last string in the array as numberString. The array size will only be 0 if s is an empty string (which would be a different problem).

And then I use Double.parseDouble() which tries to return a double value represented in a string. The try\catch block means that we are prepared that it might not work.

More information on that last bit can be found here

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download