Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Basic String to Double Convertion Problem

  1. #1
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Basic String to Double Convertion Problem

    I have no idea why I'm having so much trouble with this. It should be a very basic problem.

    Ok, so I have a GUI with a text box, where the user inputs a money amount (money is in USD). I need to convert the String amount to a double (I know I shouldn't use doubles for money, but I'm not working with "real" money, so it's not a big deal). I am attempting to create a method which can convert the String to a double, but I need to take into account the different inputs the user can do.
    My current scenarios are:
    #1: 503.21
    #2: $503.21
    #3: 503
    #4: $503
    Obviously, I need to be able to strip off the dollar sign before converting. The problem I am having is recognizing the dollar sign.
    The method I created to do the conversion is:
    public static double moneyToDouble(String s) {
    	s = s.trim();
    	String value = s.contains("\\$") ? s.replaceAll("\\$", "") : s;
    	return ValueChecker.getDoubleValue(value);
    }
    My unit tests fail for #2 and #4 (the ones with the dollar signs). The problem I am having is that the contains() method is always evaluating to false, so the $ is never replaced. I also tried with: s.contains("$"), which also failed.

    **For reference, the ValueChecker.getDoubleValue(String) method just does a normal Double.parseDouble(Strng), but defaults to 0 if the sent String is null or empty.**

    Does anyone have any advice for me?
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Basic String to Double Convertion Problem

    Would indexOf() work?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Senior Member PhHein's Avatar
    Join Date
    Mar 2013
    Location
    Germany
    Posts
    609
    My Mood
    Sleepy
    Thanks
    10
    Thanked 93 Times in 86 Posts

    Default Re: Basic String to Double Convertion Problem

    contains(CharSequence) is not a regex.
    String value = s.contains("$") ? s.replaceAll("\\$", "") : s;

    BTW: you don't need the ternary operator. If "\\$" isn't a matching pattern, the replaceAll will do nothing.

  4. #4
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Basic String to Double Convertion Problem

    Arg, PhHein was right.
    His code snippet works. I knew it was something silly I was overlooking.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

Similar Threads

  1. String to Double Conversion
    By suteki73 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: September 21st, 2013, 05:51 AM
  2. Double to string giving extra values
    By sinsand in forum Collections and Generics
    Replies: 1
    Last Post: August 23rd, 2011, 05:57 AM
  3. [SOLVED] Read double from console without having to read a string and converting it to double.
    By Lord Voldemort in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: June 26th, 2011, 08:08 AM
  4. [SOLVED] Found String Requires Double Problem.
    By NPotter86 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: January 17th, 2011, 01:24 PM
  5. string to double
    By wolfgar in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 13th, 2009, 10:47 PM