1

I'm trying to covert an array of strings into an array of doubles. I'm fairly familiar with ArrayList<>() , but i see there is in an example code give, double[].

example: list = ["1" , "2" , "3"] desired return: number = [1 , 2 , 3]

public double[] ConversionNumber
{
double[] sequence = new double[list.size()];
for(String element:list){
double val = Double.parseDouble(element);
sequence.add(val)}

return sequence;

when i do this, i get an error in Bluej compiler: "cannot find symbol- method add(double).

What is a good way to solve this (i'm a beginner at Java).

thanks!

2
  • your list is a List, not a array, right? Commented Apr 8, 2015 at 1:50
  • i created list using: ArrayList<String> list = new ArrayList<String>() Commented Apr 8, 2015 at 2:13

5 Answers 5

2

If list is an array, then list.size() would fail. I think we can assume it should be a List<String>. And you access an array by index. Also, I assume it should be an argument to your method. Next, Java convention for methods names is camelCase.

public double[] conversionNumber(List<String> list) {
    double[] sequence = new double[list.size()];
    int pos = 0;
    for (String element : list) {
        double val = Double.parseDouble(element);
        sequence[pos++] = val; // <-- post-increment
    }
    return sequence;
}
Sign up to request clarification or add additional context in comments.

2 Comments

The problem is he's calling add on an array, not the list.size() call based on his error
im assuming that because he wants a double[], list is probably a String[] not a List
1

Generally, if you're working with Collections, you work with collections the whole way. It's bad form to use Lists for one thing, but arrays for another. So it's advisable to do this with a List<Double> instead of a double[]

public List<Double> parseDoubles(List<String> strings) {
    List<Double> doubles = new ArrayList<>(strings.size());
    for(String string : strings) {
        doubles.add(new Double(string));
    }
    return doubles;
}

Comments

0

Below code will work for your case:

java.util.List<String> list = java.util.Arrays.asList("1", "2", "3");
public double[] ConversionNumber() {
    double[] sequence = new double[list.size()];
    int i = 0;
    for(String element:list){
        double val = Double.parseDouble(element);
        sequence[++i] = val;
    }
    return sequence;
}

Comments

0

The error happens because arrays dont have an 'add' method. You will want to do something like:

double[] sequence = new double[list.length]; 
for(int i = 0; i < list.length; i++){
    sequence[i] = Double.parseDouble(list[i]);
}

If list is an actual List not a String[] as I assumed, you would do:

double[] sequence = new double[list.size()]; 
for(int i = 0; i < list.size(); i++){
    sequence[i] = Double.parseDouble(list.get(i));
}

1 Comment

new double[list.length -1], the size of the double[] is not right here.
-2

Change your code here

for(String element:list){
    double val = Double.parseDouble(element);
    sequence.add(val)
}

to this if your list is a List:

for(int i=0;i<list.size();i++){
    double val = Double.parseDouble(list.get(i));
    sequence[i]=val;
}

if your list is an Array, then change to this:

for(int i=0;i<list.length;i++){
    double val = Double.parseDouble(list[i]);
    sequence[i]=val;
}

4 Comments

arrays don't have a size() method and they don't have a get() method
@Epicblood Come on, list is a List, don't you see list.size() in his codes? My answer is right absolutely
"I'm trying to covert an array of strings"
Looking at the code it might be a List, however because he wants the results in an array as well, it's safe to assume that list is an array. He tries to call add() on an array, so the size() and get() may be in error as well.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.