Take the 2-minute tour ×


  1. Each String in the String Array is type Double.

  2. Some Strings in the String Array may be null.

  3. Whenever a string in String Array is null, its corresponding Integer in Integer Array should have a value of 0.

Edit: After converting successfully I want to return this created Integer Array.

I am stuck in the places where Strings are null, getting NumberFormatException in the places where Strings are null.

Thanks in Advance.

share|improve this question
It would help if you showed the code you have so far. –  assylias Dec 31 '12 at 14:04
Without the code of your problem, you doesn't seems Code Enthusiastic.!! –  vikiiii Dec 31 '12 at 14:10
add comment

4 Answers

up vote 2 down vote accepted

So you want to convert your Strings to Integers even though they store doubles? If so then something like this should work:

    String s = stringArray[i];
        integerArray[i] = new Integer(0);
    } else {
        integerArray[i] = new Integer(s.substring(0,s.indexOf(".")));
share|improve this answer
Works as thought. Thanks. –  Code Enthusiastic Dec 31 '12 at 15:09
add comment

Before you convert the String to Double, you use an if statement to check if the String is equal to null.

If the String is equal to null, move zero to the Integer array, else do the String to Double conversion, convert the Double to an Integer, and put the Integer in the Integer Array.

share|improve this answer
add comment

Hope this helps!

    public class Test {

        public static void main(String[] args) {
            String[] strArray = {"1.1", "2.2", null, "3.3"};
            Integer[] intArray = new Integer[4];
                intArray[i] = (strArray[i] == null) ? 0 : (int) Double.parseDouble(strArray[i]);
            System.out.println("String array = ");
            for (String str : strArray) {
                System.out.print(str + " ");
            System.out.println("Integer array = ");
            for (Integer integer : intArray) {
                System.out.print(integer + " ");
share|improve this answer
Thanks for your reply. –  Code Enthusiastic Dec 31 '12 at 16:06
add comment

A simple ternary operator will do it:

String[] s = //Array instantiation
double d = new double[s.length];
    d[i] = s[i] == null ? 0 : Double.parse(s[i]);
share|improve this answer
add comment

Your Answer


