This site requires JavaScript, please enable it in your browser!
Greenfoot back
Busch2207
Busch2207 wrote ...

2011/8/16

double from String

Busch2207 Busch2207

2011/8/16

#
Hi! I've got a big problem... I try to get a decimal number from a String... For that I've used this Method:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
public static double getDouble(String ndouble)
{
    Integer i = new Integer(ndouble);
    double d;
    try
    {
        d = i.doubleValue();
    }
    catch(Exception e)
    {
        return 0;
    }
    return d;
}
If I insert "212" There's no problem and it returns 212.0. But If it try "212.2" I got this message: java.lang.NumberFormatException: For input string: "212.2" at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48) at java.lang.Integer.parseInt(Integer.java:458) at java.lang.Integer.<init>(Integer.java:660) at Numbers.getDouble(Numbers.java:13) at __SHELL9.run(__SHELL9.java:7) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:597) at greenfoot.localdebugger.LocalDebugger$QueuedExecution.run(LocalDebugger.java:260) at greenfoot.core.Simulation.runQueuedTasks(Simulation.java:406) at greenfoot.core.Simulation.maybePause(Simulation.java:318) at greenfoot.core.Simulation.runContent(Simulation.java:194) at greenfoot.core.Simulation.run(Simulation.java:187) What's wrong? Can anyone help me, plz.
Busch2207 Busch2207

2011/8/16

#
I've found a solution, so my problem is solved :)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
public static double getDouble(String ndouble)
{
    int a = ndouble.lastIndexOf(".");
    int h1 = ndouble.length()-(a+1);
    int l = ndouble.length();
    String ns = new String(new char[]{ndouble.charAt(0)});
    String hs = ns;
    for(int b=1; b<a;b++)
    {
        char ch = ndouble.charAt(b);
        if(isNumber(ch))
        {
            hs=ns;
            ns = new String(new char[]{ch});
            ns = hs+ns;
        }
        else
            return 0;
    }
    for(int b=a+1;b<l;b++)
    {
        char ch = ndouble.charAt(b);
        if(isNumber(ch))
        {
            hs=ns;
            ns = new String(new char[]{ch});
            ns = hs+ns;
        }
        else
            return 0;
    }
    int d;
    try
    {
        d = Integer.parseInt(ns);
    }
    catch(Exception e){return 0;}
    int t=1;
    for(;h1>0;h1--)
    {
        t=t*10;
    }
    return ((double)(d)/(double)(t));
}
 
public static boolean isNumber(char ch)
{
    char [] c = {'0','1','2','3','4','5','6','7','8','9','.'};
    for(int a=0; a<11;a++)
    {
        if(ch==c[a])
            return true;
    }
    return false;
}
nccb nccb

2011/8/16

#
Your original method was close, but the problem was that you were asking to turn "212.2" into an integer, and then into a double. There's no need for the integer bit, you can do:
1
2
3
4
5
6
7
8
9
10
11
public static double getDouble(String ndouble) 
    try
    {
        return Double.parseDouble(ndouble);
    }
    catch (NumberFormatException e)
    {
        return 0;
    }
}
Busch2207 Busch2207

2011/8/16

#
xD Ok. Thank you very much! :) That's much shorter than my solution. x)
You need to login to post a reply.