0

I am trying to read a line of numbers in string datatype then parse them into double before reading the next line. I get a nullpointerexception.

I am trying to read numbers 4 5 2 first, parse them to double, then do some other calculations before going to the second line of number which is 1 1 3 1.

        double num;
        String delim;
        String[] tokens = null;
        double[] numArray = null;
        String data = "4 5 2\n1 1 3 1\n";

        Scanner input = new Scanner(data);

        while(input.hasNextLine())
        {
            delim = "[ \n]+"; //separate by spaces
            tokens = data.split(delim);

            for(int i = 0; i < tokens.length; i++)
            {
                num = Double.parseDouble(tokens[i]);
                numArray[i] = num;
                System.out.print(numArray[i]); //
            }

        }
4
Contributors
3
Replies
15
Views
2 Years
Discussion Span
Last Post by Taywin
0

Try checking what's the size of your numArray and show stacktrace as stultuske alrdy asked for

Edited by Slavi

0

The back slash in your delim should be escaped because it is supposed to be a regex String. However, a better regex should be \\s+ instead...

Edited by Taywin

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.