开发者

Convert array of strings to array of double in java [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 10 years ago.
import java.util.Scanner;

import java.lang.String;

public class SA3

{

    public static void main(String[] args)
    {


        Scanner scan = new Scanner(System.in);

        System.out.print("Enter student record : ");

        String scores = scan.nextLine();

        String[] StringOfMarks = scores.split(","开发者_如何学Python);
        double[] Marks = new double[StringOfMarks.length];


         for(double i = 0; i < StringOfMarks.length; i++)
         {
             Marks[i] = StringOfMarks[i];
         }
      }

}


Change the last part of your code into

for(int i = 0; i < StringOfMarks.length; i++)
{
    Marks[i] = Double.parseDouble(StringOfMarks[i]);
}

You need to use an int typed variable for array element access and need to cast the String explicitly into double.


This converts a single array element, not a whole array.

Also, what is the type of Marks? If it's not double[] you're likely to see that "loss of precision" warning.


This should not affect the precision of your doubles as long as it fits the Java double type. You should also bear in mind, that not every value of double can be represented.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜