Okay, so you can't use ifs, that means you have to use integer arithmetic.
Think of it this way: if you need to give $1.52 in quarters, dimes, nickels and pennies, one way to do it would be to take .25 at a time out of that sum until there was less than a quarter. After that, you can take dimes until you're under a dime, then you take as many nickels as you can, then you give one penny at a time.
Looking at your post above - don't try to do this with Strings. Make yourself an integer. $5.26 is 526 cents, do your calculation on that integer value. It'll be much easier to deal with. (you can modify the procedure I outlined to deal with dollars pretty easily as well)
Have fun, come back with your code or on it.
This is an inefficient procedure, but I think you can probably write it easily enough, and without any ifs (or ands or buts, for that matter!)
Once you've written that, we can talk about improving it.
By the way, if you do need to convert:
Most primitives have a wrapper-class toString() method. int->String: Integer.toString(i); Most primitives also have a conversion from String method as well. String->int: Integer.parseInt(i); Therefore, for something like char to int: Integer.parseInt(Character.toString(c));
Cool. The fact that you worked out the division part from what you were given is pretty good, it shows that you're thinking about what you're doing. Good work.
You should probably take a little time to look at floating point arithmetic at some stage. Floating point representation in computers is necessarily inexact, and you might run into trouble with comparison of values. It's possible, for example, that two values which should be equal will differ somewhere out in the godknows-th decimal place - and then they won't be equal. For discrete quantities like money, it's usually best to use ints, to avoid these errors.
Hi, as I was told that my code doesn’t scale well at all, I thought perhaps I’d try to get a better understanding of interfaces/abstract classes and classes and the relationship between them.
I don’t want at this stage work on a big separate project as I've already got plenty ...
I am writing a java program that needs to execute shell commands, so I wrote a function that would take the command to execute as a string (ie: "mkdir ~/Folder1") and execute that command with the shell. Here is the function:
Runtime run = Runtime.getRuntime();
Process pr = ...
Hi. I have a form with list box : lst_product, datagridview : grd_order and button: btn_addline. lst_product has a list of product ids selected from database (MS Acess 2013) , grd_order is by default empty except for 2 headers and btn_addline adds rows to grd_order.