[Java] BankBalance

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
  import java.util.Scanner;
import javax.swing.JOptionPane;

public class BankBalanceGUI
{
	public static void 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 >> ");
			}
	}
}
Inside your loop, you need to set the value of response.
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
Last edited on
Topic archived. No new replies allowed.