Search code examples
javaarraysdoublesequences

How do I put a String into an array and then into an array of double?


Currently I have this:

public class sequence
{
private double[] sequence; 
// set up sequence by parsing s
//the numbers in s will be seperated by commas
public Sequence(String s)
{
  String [] terms = s.split (",");
  sequence = Double.parseDouble(terms);
}
}

What I have does not work. But basically what i am trying to achieve is to move the numerical terms in String s (such as 1,2,3,4,5,6) in an array of double called sequence.


Solution

  • Double.parseDouble take one String and returns one Double. You are passing an array of Strings. Change you code to pass just one String.

    public Sequence(String s) {
        String [] terms = s.split (",");
        sequence = new Double[terms.length];
        for (int i = 0; i < terms.length; i++) {
            sequence[i] = Double.parseDouble(terms[i]);
        }
     }