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(",");
double[] Marks = new double[StringOfMarks.length];
for(double i = 0; i < StringOfMarks.length; i++)
{
Marks[i] = StringOfMarks[i];
}
}
}
views:
114answers:
3
A:
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.
Oak
2010-05-16 18:28:14
it is double ..
WM
2010-05-16 18:37:17
@WM: in that case we really need an SSCCE (http://sscce.org/) or at least some more code.
Oak
2010-05-16 18:52:19
A:
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.
Gabriel Ščerbák
2010-05-16 18:28:40
+2
A:
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.
Timo Westkämper
2010-05-16 19:09:17
Also better use variable names starting with a lower case letter which is a convention for Java.
Timo Westkämper
2010-05-16 19:28:49