Convert array of strings to array of double in java [closed]
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.
精彩评论