String bla = "12.25";
double showWithdrawal = 0;
try {
showWithdrawal = Double.parseDouble(bla);
} catch (Exception e) {
e.getMessage();
}
System.out.println(showWithdrawal);
You can do like that
Question
Please is this being smart or stupid..i tried to return a String in a method of type double..
public double withdrawCash()
{
if (amounts == 20)
{
totalWithdrawal = BALANCE - 20;
return totalWithdrawal;
}else if (amounts == 40){
totalWithdrawal = BALANCE - 40;
return totalWithdrawal;
}else if (amounts == 60){
totalWithdrawal = BALANCE - 60;
return totalWithdrawal;
}else if (amounts == 100){
totalWithdrawal = BALANCE - 100;
return totalWithdrawal;
}else if (amounts == 200){
totalWithdrawal = BALANCE - 200;
return totalWithdrawal;
}
else{ //this where i tried to be smart or stupid...just learning..help!!!
String complainWithdrawal = "Please, you need to select appropriate amount";
double showWithdrawal = Double.parseDouble(complainWithdrawal);
return showWithdrawal;
}
}
Kindly advice me on the ideal solution
Solution 2
String bla = "12.25";
double showWithdrawal = 0;
try {
showWithdrawal = Double.parseDouble(bla);
} catch (Exception e) {
e.getMessage();
}
System.out.println(showWithdrawal);
You can do like that
OTHER TIPS
double showWithdrawal = Double.parseDouble(complainWithdrawal);
This line will throw an error. You cannot return a String as a double. Instead, you can replace it with:
throw new InputMismatchException("Please, you need to select appropriate amount.");
And catch it outside like this:
try {
withdrawCash();
} catch (InputMismatchException e) {
System.out.println("Error withdrawing cash: " + e);
}
Can I also suggest that for money you don't use double. Take a look at BigDecimal as well. There are a lot of issues you will run into down the road using double for monetary values.