Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Re: Trying to wrap my head around Java

  1. #1
    Member
    Join Date
    Oct 2012
    Posts
    33
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Trying to wrap my head around Java

    In order to keep the amount of threads I post down. I have a new assignment that I'll just post here, unless that's against the rules? Anyway here it goes.

    My assignment is to design a program that takes three user inputs. One for the amount invested, one for the amount of years of the investment, and then another for the amount of interest.

    I've got this so far:
     
    import javax.swing.JOptionPane;
    public class Investment
    {
    	public static void main(String[] args)
    	{
    		double Total, investment, interest, intPerc;
    		String stInvestment = JOptionPane.showInputDialog(null, "Enter the amount Invested: ");
    		investment = Double.parseDouble(stInvestment);
    		if (investment<=0)
    			{
    				JOptionPane.showMessageDialog(null, "You've entered an invalid amount for your Investment.");
    				System.exit(-1);
    			}
    		String stYears = JOptionPane.showInputDialog(null, "Enter the amount of years of the Investment: ");
    		int years = Integer.parseInt(stYears);
    		if (years<=0)
    			{
    				JOptionPane.showMessageDialog(null, "You've entered an invalid amount for the amount of years of your Investment.");
    				System.exit(-1);
    			}
    		String stInterest = JOptionPane.showInputDialog(null, "Enter the Percent of interest as a whole number: ");
    		interest = Double.parseDouble(stInterest);
    		while(years>0)
    			{
    				int time = years*12;
    				intPerc = interest / 100;
    				Total = ((investment * intPerc) + investment);
    				System.out.println("You're investment after " + time + " months amounts to " + Total);
    			years = years - 1;
    			}
    	}
    }

    I have to problems, the main problem is that the output is incorrect, instead of applying the interest earned towards the new investment each time, it outputs the same amount each time.

    This is an example of the output at 1000 at 2 years, with 3% interest.
    You're investment after 24 months amounts to 1030.0
    You're investment after 24 months amounts to 1030.0

    Second problem isn't so much a problem as a question of interest. (Err, now looking at the code that I posted this doesn't have the keyboard.) but I have a second code that I'm working on using a scanner.)
    		Scanner keyBoard = new Scanner(System.in);
    		System.out.println("Enter the amount Invested: ");
    		double investment = keyBoard.nextInt();
    At the first line I get a type of error saying that "Resource leak: 'keyboard' is never closed".

    Just curious as to how to fix this. If you need the entire code I can post it, but in order to keep confusion down I only posted the bit that had the error.


  2. #2
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Trying to wrap my head around Java

    In order to keep the amount of threads I post down. I have a new assignment that I'll just post here, unless that's against the rules? Anyway here it goes.
    It is not really against the rules so to speak, but a new question deserves a new thread. If someone else has the same question, a new thread will make searching the forum for this easier. So I moved this post to a new thread.



    If you need the entire code I can post it, but in order to keep confusion down I only posted the bit that had the error.
    Posting code related to the question, along with the question, leaves it available for anyone searching the forum. It is not a problem to post the code.



    You did a test run on 2 years, and see the same results. 2 is such a small number. When comparing half of 2 to the other half of 2, how will you know which half is correct? Try a larger test set to identify a pattern. This will help you find the error. Perhaps run a test for 10 years, or 20 even. The computer does not care (too much) how much work is to be done.

Similar Threads

  1. Trying to wrap my head around Java
    By Sylis in forum What's Wrong With My Code?
    Replies: 4
    Last Post: October 10th, 2012, 08:10 AM
  2. how to wrap a pre-generics library
    By frumious in forum Object Oriented Programming
    Replies: 2
    Last Post: April 9th, 2012, 09:58 PM
  3. Need urgent help regarding java word wrap function.. URGENT
    By coldice in forum What's Wrong With My Code?
    Replies: 3
    Last Post: August 16th, 2011, 05:43 AM
  4. Why wont the Button wrap?
    By Taylorleemusic in forum What's Wrong With My Code?
    Replies: 4
    Last Post: October 1st, 2010, 12:03 AM
  5. Simple Game In Java (Head and Tails).
    By drkossa in forum Java Theory & Questions
    Replies: 5
    Last Post: November 28th, 2009, 11:40 AM