While I know this is a forum dedicated to C++ Can you either point me towards a forum dedicated to Java beginner like myself or any help would be appreciated.
Currently I'm working on a gui program for bank interest. At the end of the program the user is to input 1 or another number to continue on to another year. This part of the program goes on in an infinite loop. I need to figure out how to stop the loop. This is code I modified to make into a GUI applet
import java.util.Scanner;
import javax.swing.JOptionPane;
publicclass BankBalanceGUI
{
publicstaticvoid main(String[] args)
{ String one;
String two;
double balance;
int response;
int year = 1;
final double INT_RATE = 0.03;
Scanner keyboard = new Scanner(System.in);
one = JOptionPane.showInputDialog("Enter initial bank balance > ");
// balance = keyboard.nextDouble();
two = JOptionPane.showInputDialog("Do you want to see next year's balance?" + "Enter 1 for yes" + " or any other number for no >> ");
balance = Integer.parseInt(one);
response = Integer.parseInt(two);
while(response == 1)
{
balance = balance + balance * INT_RATE;
JOptionPane.showMessageDialog(null, "After year " + year + " at " + INT_RATE +
" interest rate, balance is $" + balance);
year = year + 1;
two = JOptionPane.showInputDialog("\nDo you want to see the balance " + "at the end of another year?"
+ "Enter 1 for yes" + " or any other number for no >> ");
}
}
}
Another option is: while(two.equals("1"))
I don't see a point converting the input to int, also it would be more user friendly to let the user type "yes" or "no".
Probably the best option is to use showConfirmDialog. https://docs.oracle.com/javase/7/docs/api/javax/swing/JOptionPane.html
That's from the original code. The original program didn't use JOptionPane, which is why the code still has scanner keyboard. I was trying to see if I could turn it into a GUI interface, but so far we've only touched on JOptionPane.showMessageDialog()
Inside your loop, you need to set the value of response.
This is what i was thinking if i should set it to response=Integer.parseInt(two)
inside of the while loop
Actually I'm still surprised that the increment got passed to showMessageDialog