0

I'm working on a smart EditText where the user write a product and a price without any worries about formatting (e.g. "Pasta 0,50" or "0.50 tomato juice").

I'd like to separate the price from all the rest to have something like

String input = "Cheese 2,00 for Pizza";
String outputProduct = "Cheese for pizza";
String outputPrice = "2,00";

Usually it shouldn't be a thing, but I'm also considering of taking care if the price isn't at the very start or the very end of the string, so if it's in the middle.

I also have to deal with periods and comms in prices.. since not everyone use the same kind of format.

What I've tryed so far: answer from OscarRyz here but it seems to be not very related with what I'm trying to achieve. Also seen some array operations but my problem is to get the actual length of the price.

Any help would be very appreciated!

Community
  • 1
  • 1
Lampione
  • 1,622
  • 3
  • 21
  • 39
  • 2
    Try using a regex to extract the numeric part fron the string. – Phantômaxx Feb 17 '15 at 19:32
  • 2
    some people write 2,000 as 2 thousand. Did you consider that? It will be extremely buggy to let the user decide what format to use – Ahmed Hegazy Feb 17 '15 at 19:34
  • possible duplicate of [Java Regular Expression removing everything but numbers from String](http://stackoverflow.com/questions/6883579/java-regular-expression-removing-everything-but-numbers-from-string) – Codeman Feb 17 '15 at 19:34
  • This is doomed to failure. Think again please. Natural language parsing needs data centres, and even Google don't have it right yet. – Simon Feb 17 '15 at 19:45
  • My advice is to separate the price field and the product field as two edit texts. You can do specific formatting and input limiting on the price edit text to get consistent output. These other guys are correct even if you get it working with certain types of prices there will still be bugs and not all cases will be covered, like if you type out the price in words. Then what happens if it's Two cheese pizzas for Six Dollars? – Dave S Feb 17 '15 at 19:47
  • 1
    You're right (all). Didn't think about this kind of scenario. What I'm actually doing is using two separate fields and I'm ok with this. Thank you all. – Lampione Feb 17 '15 at 19:57

0 Answers0