Search code examples
javaswingjoptionpane

JOptionPane Can Not DIsplay Everything


Hello SO Community I have recently changed my program from a Scanner input and display to a JOptionPane input and display, It is set up to display the investment over a course of user specified years

Currently if the number of years is more than 3 it will terminate the program, how would I go about solving this and/or creating a single pop up window to show the results? Thank you in advance everyone.

import java.util.*;
import javax.swing.JOptionPane;

public class Investing {

public static void main (String[] args){

    double amount;

    double principal;

    double rate;

    Scanner input1 = new Scanner(System.in);

    String principals = JOptionPane.showInputDialog("Please Enter The Principal");
    principal = Double.parseDouble(principals);

    String rates = JOptionPane.showInputDialog("Please Enter The Rate Of Interest");
    rate = Double.parseDouble(rates);

    String years = JOptionPane.showInputDialog("Please Enter The Number Of Investment Years");
    double g = Double.parseDouble(years);
    String zs = JOptionPane.showInputDialog( "Please Enter Annual Contribution");
    double z = Double.parseDouble(zs);

    amount = principal;
    for(int year = 1; year <= g; year++){
        amount *= 1 + rate/100;
        amount+= z;
        JOptionPane.showMessageDialog( null, year + "  " + amount, zs, year);
    }
}
}

Solution

  • You are using JOptionPane wrongly

    JOptionPane.showMessageDialog( null, year + "  " + amount, zs, year);
    

    You are passing year as message type, so it fails after year 3. you should do

    JOptionPane.showMessageDialog( null, year + "  " + amount, "title", JOptionPane.INFORMATION_MESSAGE );