Results 1 to 6 of 6
  1. #1
    Join Date
    Aug 2009
    Posts
    3
    Rep Power
    0

    Exclamation 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.

    Java 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);
    	}
    }
    Last edited by javalearner1234; 08-17-2009 at 03:49 AM.

  2. #2
    Mr.Beans's Avatar
    Mr.Beans is offline Senior Member
    Join Date
    Apr 2009
    Location
    Virginia Beach, Virginia
    Posts
    149
    Rep Power
    0

    Default

    Are you looking for a drop down menu class?

    Try JComboBox

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Your JOptionPane.showInputDialog returns a String. Have you had a look at the String returned? If not, you should...
    Java 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;
                }

  4. #4
    Join Date
    Aug 2009
    Posts
    3
    Rep Power
    0

    Default

    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

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Thanks fubarable-- I added your line and it did not help.
    My line was not a direct solution to your problem but to allow you to see what that String held. If you know what the String holds, you should be smart enough to compare it with the source Strings in the sample array and thereby come up with a solution, no?

  6. #6
    Join Date
    Aug 2009
    Posts
    3
    Rep Power
    0

Similar Threads

  1. Replies: 1
    Last Post: 04-01-2009, 10:28 AM
  2. passively and quickly capturing request/response
    By sideswipe091976 in forum Networking
    Replies: 1
    Last Post: 07-10-2008, 01:01 AM
  3. Capturing video with Robot
    By russ2620 in forum AWT / Swing
    Replies: 0
    Last Post: 06-03-2008, 04:49 PM
  4. Capturing Groups using regular expressions
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-25-2007, 11:19 AM
  5. MAC address capturing
    By viswa.tk in forum Java Applets
    Replies: 0
    Last Post: 12-13-2007, 08:36 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •