Java exception.

This line, perhaps?
Code:
two = Integer.parseInt(priceperlitrestring.substring(2,3));

If an if statement has more than one line within it's block, you should encapsulate with {}. So for example:
Code:
if (priceperlitrestring.substring(1,2)==".")
one = Integer.parseInt(priceperlitrestring.substring(0,1));
two = Integer.parseInt(priceperlitrestring.substring(2,3));
three = Integer.parseInt(priceperlitrestring.substring(3,4));
four = 0;
should read:
Code:
if (priceperlitrestring.substring(1,2)==".") {
  one = Integer.parseInt(priceperlitrestring.substring(0,1));
  two = Integer.parseInt(priceperlitrestring.substring(2,3));
  three = Integer.parseInt(priceperlitrestring.substring(3,4));
  four = 0;
}
 
Last edited:
Why are the numbers stored as Strings? If you have them as floats or whatever a dynamic cast to an int should be adequate.
 
Should do, yeah. You'd pass in 0 as the digit if you wanted the digit immediately to the left of the decimal point, -2 if you wanted the digit two places to the right, 4 if you wanted the digit 4 places to the left, and so on.
 
The problem is I won't know at the time whether there will be 3 or 6 digits in a number, so if say digit 5 is set to -2 and there isn't 2 digits after the decimal point won't that result in an exception?
 
just convert the double to a string then do String.charAt(), then check if it is a "." and if it is don't parse in into an Integer. this can be done in a while loop so you won't require those 3 if statements.
 
The problem is I won't know at the time whether there will be 3 or 6 digits in a number, so if say digit 5 is set to -2 and there isn't 2 digits after the decimal point won't that result in an exception?

Nope, it'll just return 0. You could pass -43 or 76 or something similar in and it'd just return 0, as you'd expect it to. There's nothing in there that could actually produce an error, so you're OK.
 
Back
Top Bottom