# help capturing input from drop down

• 08-17-2009, 03:46 AM
javalearner1234
help capturing input from drop down
Working on an assignment and just need a little help or point in the right direction to capture the information in the drop down. I was able to capture the text if it is manually entered but using the drop down is messing with me. I can either use a drop down or select the 3 options from check boxes.

Code:

```import java.io.*; import java.awt.*; import java.text.DecimalFormat;    //import java.text for decimal formatting import java.lang.Math;            //import java math class for formulas and calculations import javax.swing.JOptionPane;    //import a Java swing components for display messages and accept user input via dialog boxes public class Mortgage_Calc {         public static void main(String[] args)  //method header         {       //declare variables                 int numMonths,iVar,iMenuInt;                 int numYears=0;                 int years[] = {7,15,30};    //static number of years                 double intRates[]={.0535,.055,.0575};  //static interest rates                 double loanAmount,monthlyPayment,newloanAmount,curMInterest,curMPrincipal;                 double annualInterestRate=0.0;                 double monthInterestRate=0.0;                 String totalLoan,interestLoan,pmtSched,iMenu,termLoan;                 String sample[] = {"7 years at 5.35%","15 years at 5.5%","30 years at 5.75%"};                             //set decimal formats                 DecimalFormat money = new DecimalFormat("\$###,###.00");                 DecimalFormat percent = new DecimalFormat("#.00%");                                 //start loop                 do                 {                         iMenu=JOptionPane.showInputDialog(null, "Option 0 - Exit program.\nOption 1 - To input amount, then select a predetermined term, and interest rate.\n\nenter 1 to run program or O (zero) to quit");                         iMenuInt = Integer.parseInt(iMenu);                                                 if (iMenuInt>0)                         {                                 //ask for loan amount                                 totalLoan=JOptionPane.showInputDialog(null, "What is the amount of the mortgage?\n\nexample (\$200,000 = 200000)  do not enter , or \$");                                 loanAmount = Double.parseDouble(totalLoan);                                                                 switch (iMenuInt)                                 {                                         case 1:                //display dropdown selection                                         String option = (String)JOptionPane.showInputDialog(null,"Which loan option?","Options",JOptionPane.QUESTION_MESSAGE,null,sample,sample[sample.length-1]);                                         break;                                 }                                 monthInterestRate=annualInterestRate/12;  //interest rate broken down by month                                 numMonths = numYears*12;                  //converts years to months                                 monthlyPayment=loanAmount*(monthInterestRate/(1-(Math.pow((1+monthInterestRate),0-numMonths))));    //monthly payment formula                                 //monthly payment output                                 JOptionPane.showMessageDialog(null,"A mortgage loan of " + money.format(loanAmount) + " for " + numYears +" years \nwith an interest rate of " + percent.format(annualInterestRate) + " will result in a\nmonthly payment of " + money.format(monthlyPayment) + " for the life of the loan.\n\nPlease click OK to see the amortization schedule.","Thank you!",JOptionPane.INFORMATION_MESSAGE);                                 //amortization output                                 iVar=0;                                 pmtSched="\nMonth            ";                                 pmtSched+="Balance                  ";                                 pmtSched+="Interest            ";                                 pmtSched+="Principal\n";                                 do                                         {                                                 iVar++;                                                 curMInterest=loanAmount*monthInterestRate;    //payment that is applied  to the interest on the loan                                                 curMPrincipal=monthlyPayment-curMInterest;    //payment that is applied to the principal of the loan                                                 newloanAmount=loanAmount-curMPrincipal;      //remaining balance of the loan at the end of the month                                                 pmtSched+=String.valueOf(iVar)+"                    ";                                                 pmtSched+=money.format(loanAmount)+"          ";                                                 pmtSched+=money.format(curMInterest)+"          ";                                                 pmtSched+=money.format(curMPrincipal)+"\n";                                                 loanAmount=newloanAmount;                                                                                                 if ( iVar % 25 == 0) {                                                 pmtSched+="\n\n\nMonth            ";                                                 pmtSched+="Balance                  ";                                                 pmtSched+="Interest              ";                                                 pmtSched+="Principal\n";                                         }                                 } while (iVar<numMonths);                                 JOptionPane.showMessageDialog(null,new TextArea(pmtSched,30,60),"Your Loan Payment Amortization Schedule",JOptionPane.INFORMATION_MESSAGE);                         }                 }                while (iMenuInt>0);                         System.exit(0);         } }```
• 08-17-2009, 03:56 AM
Mr.Beans
Are you looking for a drop down menu class?

Try JComboBox
• 08-17-2009, 04:00 AM
Fubarable
Your JOptionPane.showInputDialog returns a String. Have you had a look at the String returned? If not, you should...
Code:

```            switch (iMenuInt) {               case 1: // display dropdown selection                   String option = (String) JOptionPane.showInputDialog(null,                           "Which loan option?", "Options",                           JOptionPane.QUESTION_MESSAGE, null, sample,                           sample[sample.length - 1]);                   // !! I added this.  See what's returned.  Perhaps you can use it.                   System.out.println("Option selected: " + option);                                     break;             }```
• 08-17-2009, 04:10 AM
javalearner1234
Thanks fubarable-- I added your line and it did not help. I am thinking I am going to have to start over on this. My last assignment just wanted everything manually entered, which worked. So I was just trying to tweak that program to work from a drop down. The 3rd assignement just combines both of these options so that is easier if I can get past this.

thanks again
• 08-17-2009, 04:40 AM
Fubarable
Quote: