-1

I am new to Java and Android, so I really don't know all the codes and syntax. And I have to convert String to Double. For example if I have a string:

Double dob1,dob2;
String str = "12345,56789";

Now I have to convert this into double format and want the values to be stored like:

dob1 = 12345
dob2 = 56789

How can I do this?

Michael Petrotta
  • 59,888
  • 27
  • 145
  • 179
  • Have you done any research? This is pretty easy if you split it into several smaller problems, i.e. splitting a `String` and converting a `String` into `double`. – awksp Jul 05 '14 at 06:32

6 Answers6

3

If I understand you, you could do something like this -

String str = "12345,56789";
String[] arr = str.split(",");
Double dob1 = Double.valueOf(arr[0]);
Double dob2 = Double.valueOf(arr[1]);
System.out.printf("dob1=%f, dob2=%f%n", dob1, dob2);
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
2

Basic way of doing it

    Double dob1,dob2;
    String str = "12345,56789";

    String[] strSplit = str.split(",");

    dob1 = Double.parseDouble(strSplit[0]);
    dob2 = Double.parseDouble(strSplit[1]);
vinayknl
  • 1,242
  • 8
  • 18
0
 StringTokenizer st = new StringTokenizer(str, ",");
 dob1 = Double.parseDouble(st.nextToken());
 dob2 = Double.parseDouble(st.nextToken());

You can also use String.split method instead of the tokenizer.

Audrius Meškauskas
  • 20,936
  • 12
  • 75
  • 93
  • 1
    From the Javadoc for `StringTokenizer` - "StringTokenizer is a legacy class that is retained for compatibility reasons although its use is discouraged in new code." – Dawood ibn Kareem Jul 05 '14 at 06:37
  • It is not officially deprecated and would not be a reason for that. I myself see switching basically into another language (Java regexp) unnecessary. The user must know all regexp reserved characters and how to escape them, and it will not be compiler error. It is possible also to do with String.split if ones religion or company coding rules demand. – Audrius Meškauskas Jul 05 '14 at 06:49
0
dob1= Double.parseDouble(str.substring(0, str.lastIndexOf(",")));
        dob2= Double.parseDouble(str.substring(str.lastIndexOf(",")+1,str.length()));
SHASHIDHAR MANCHUKONDA
  • 3,302
  • 2
  • 19
  • 40
0

Try this code, You can input valus in to a double array

    String s1 = "1256,125662";

    String []arr=s1.split(",");
    double []numArr =new double[arr.length];
    for (int i = 0; i < arr.length; i++) {
        numArr[i] = Double.parseDouble(arr[i]);

    }
user3717646
  • 436
  • 3
  • 10
0

Other people have already answered how to do this, but if you want to make this a more dynamic process (being able to handle more than 2 numbers without explicitly writing out the code for each), here is an example of such a method:

public static double[] getDoublesSeperatedByCommas(String textWithNumbers) {
    String[] textParts = textWithNumbers.split(",");
    double[] returnDoubles = new double[textParts.length];
    for (int i = 0; i < textParts.length; i++) {
        if (textParts[i].isEmpty())
            returnDoubles[i] = 0;
        else
            returnDoubles[i] = Double.parseDouble(textParts[i]);
    }
    return returnDoubles;
}

That method will take a String (like the one you specified) and return a double array with all the specified values which were separated by commas. I also added a simple check to see if the strings were empty (eg: "245,5653,,346")

-Thomas

T-Fowl
  • 704
  • 4
  • 9