Results 1 to 5 of 5
  1. #1
    Mekonom is offline Member
    Join Date
    Nov 2009
    Posts
    71
    Rep Power
    0

    Default adding in array String

    Hi ... I have an String like +123.321 and I want to add it to an array ..My array like this int[] coordinates = {0,0,0} ..I made it int because when I print ,it must give 0 value if I did not add some thing.. I know that I must conver this String to some int or float but there is a sign + or - and when converting it gives me error .What must I do?

  2. #2
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,429
    Rep Power
    7

    Default

    How do you convert?
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  3. #3
    Mekonom is offline Member
    Join Date
    Nov 2009
    Posts
    71
    Rep Power
    0

    Default

    like this

    Java Code:
     String string_to_float="1234.89";
    
            float flt=Float.parseFloat(string_to_float);

  4. #4
    Mekonom is offline Member
    Join Date
    Nov 2009
    Posts
    71
    Rep Power
    0

    Default

    but it gives error if there is some + or - or something like +1234.43

  5. #5
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,429
    Rep Power
    7

    Default

    Ok, first of all you should post the exact error, because "it gives error" is not very descriptive. This works fine for me:
    Java Code:
    	
    	String string_to_float1="+1234.89";
    	String string_to_float2="1234.89";
    	String string_to_float3="-1234.89";
            float flt1=Float.parseFloat(string_to_float1);
            float flt2=Float.parseFloat(string_to_float2);
            float flt3=Float.parseFloat(string_to_float3);
            System.out.println(flt1);
            System.out.println(flt2);
            System.out.println(flt3);
    Output:
    1234.89
    1234.89
    -1234.89
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

Similar Threads

  1. adding to an array
    By mayhewj7 in forum New To Java
    Replies: 14
    Last Post: 02-19-2009, 06:41 AM
  2. Adding numbers in an array?
    By hawaiifiver in forum New To Java
    Replies: 9
    Last Post: 01-22-2009, 04:50 AM
  3. adding <br> html tag to string
    By newbieal in forum New To Java
    Replies: 7
    Last Post: 10-14-2008, 03:51 PM
  4. Replies: 1
    Last Post: 08-03-2007, 11:52 AM
  5. Adding graphics to array
    By romina in forum Java 2D
    Replies: 1
    Last Post: 08-01-2007, 02:45 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •