Results 1 to 8 of 8
  1. #1
    r0sedeanne's Avatar
    r0sedeanne is offline Member
    Join Date
    Sep 2011
    Location
    Honolulu, Hawaii
    Posts
    9
    Rep Power
    0

    Default Did I do this right? (Interest calculator)

    There aren't any errors coming up, and the math part runs fine, I just want to make sure that this does what it's supposed to. I feel like I'm missing something.

    What do you think? These were the instructions:
    1. Ask the user for initial principal amount and annual percentage rate.
    2. Program will go through the for-loop but instead of displaying in the screen, write to a text file.
    3. Program will then open the same text file, read the data and append to the outputTextArea variable and then JOption display on the screen inside a JOptionPane message dialog.


    Java Code:
    import java.util.Scanner;
    import java.io.*;
    import java.text.NumberFormat;	//class for numeric formatting
    import java.util.Locale;	//class for country-specific information
    import javax.swing.JOptionPane;
    import javax.swing.JTextArea;
    
    public class Interest3
    {
    	public static void main(String[] args) throws IOException
    	{
    		double amount,		//amount of deposit at end of each year
    			principal,	//initial amount before interest
    			rate;		//rate of interest
    		String input;
    
    	//create NumberFormat for currency in US dollar format
    	NumberFormat moneyFormat = NumberFormat.getCurrencyInstance( Locale.US );
    
        	//create keyboard
        	Scanner keyboard = new Scanner(System.in);
    
        	//create JTextArea to display output
        	JTextArea outputTextArea = new JTextArea();
    
        	input = JOptionPane.showInputDialog("Please enter Principal: ");
        	principal = Double.parseDouble(input);
    
        	input = JOptionPane.showInputDialog("Please enter Interest Rate (Format: 0.00) ");
        	rate = Double.parseDouble(input);
    
        	//set first line to display output
        	outputTextArea.setText("Year\tAmount on deposit\n");
    
    	//open new file
        	FileWriter fwriter = new FileWriter("MyMoneys.txt", true);
        	PrintWriter outputFile = new PrintWriter(fwriter);
    
        	//calculate amount on deposit for each of ten years
        	for (int year = 1; year<= 10; year++)
        	{
        		amount = principal * Math.pow(1.0 + rate, year);
    
    		// append one line of text to outputTextArea
    		outputTextArea.append( year + "\t" +
    		moneyFormat.format(amount) + "\n");
        	}// end for
    
        	//close file
        	outputFile.close();
        	System.out.println("Data written to file");
    
        	//display results
        	JOptionPane.showMessageDialog(null, outputTextArea, "Compound Interest", JOptionPane.INFORMATION_MESSAGE);
    
        	System.exit(0);  //terminate the application
    
        }//end of main
    
    }//end of Interest3.java
    Last edited by r0sedeanne; 09-13-2011 at 09:32 AM.

  2. #2
    r0sedeanne's Avatar
    r0sedeanne is offline Member
    Join Date
    Sep 2011
    Location
    Honolulu, Hawaii
    Posts
    9
    Rep Power
    0

    Default Re: Did I do this right? (Interest calculator)

    Besides the wonky indents... They never seem to transfer over well.

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

    Default Re: Did I do this right? (Interest calculator)

    Quote Originally Posted by r0sedeanne View Post
    Besides the wonky indents...
    You should take care to fix these before posting as you really want it easy for others to read, especially if you're trying to convince them to help you.

    There aren't any errors coming up, and the math part runs fine, I just want to make sure that this does what it's supposed to. I feel like I'm missing something.
    The bottom line is, when you run it, does it run as expected? Do you get the correct output for your input? This is something you should test before posting as we're not the best substitute for your compiler and JVM.

    Luck.

  4. #4
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default Re: Did I do this right? (Interest calculator)

    I agree with fubarable.

    Do you think it's right? If so; why? If not, why not? The more specific a question you ask the more help you will receive. I haven't read the actual code yet(because I'm lazy, and doing some other stuff simultaneously), but I will get to it and give you some advice(which will probably have more to do with style, and less to do with the completeness of the assignment). Compiling and debugging to get the right results is definitely an important task to learn. All of coding is experience and repetition, the more you practice, the more you see and fix the errors, the easier it will become.

  5. #5
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default Re: Did I do this right? (Interest calculator)

    Now that I have read the code; some comments/critiques.

    What's the point of crating a Scanner and then never using it? It's a waste, either use the scanner instead of the input dialog, or get rid of the scanner.

    Next, there appear to be some errors; use the compiler to find and correct the errors if you cannot readily find them yourself. The compiler will spit out errors which are quite useful. They will specify what went wrong, and what line the problematic area is.

    You have a JTextArea, but it is not put in any container; what happens if you compile the following code

    Java Code:
    import javax.swing.*;
    public class X{
      public static void main(String[] args){
        JTextArea text = new JTextArea();
        text.setText("Kurt Gödel says -- \"Boogedy boogedy boo!\"");
      }
    }
    What happens when you run this code? This same problem is found in your code snippet.

    I hope this helps you spot some errors. Please feel free to ask any further questions you may have an I (as well as others) will try to help you.

  6. #6
    r0sedeanne's Avatar
    r0sedeanne is offline Member
    Join Date
    Sep 2011
    Location
    Honolulu, Hawaii
    Posts
    9
    Rep Power
    0

    Default Re: Did I do this right? (Interest calculator)

    Next, there appear to be some errors; use the compiler to find and correct the errors if you cannot readily find them yourself. The compiler will spit out errors which are quite useful. They will specify what went wrong, and what line the problematic area is.
    I wrote it using JCreator and the code runs fine, no errors, it tells me what I wanted it to, but I don't actually think it's writing anything to the text file. And that's going to be an issue when I turn it in for grading. I don't know what I'm missing, and I'm teaching myself out of a textbook (teacher rarely ever logs in to answer questions), so please try to excuse any really dumb mistakes you find in there. Can't really say I know what I'm doing.

    So. I should just delete "Scanner keyboard = new Scanner(System.in);" then? And what do I need to do to get it to use data from the text file and not just from memory?

  7. #7
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,240
    Rep Power
    19

    Default Re: Did I do this right? (Interest calculator)


  8. #8
    r0sedeanne's Avatar
    r0sedeanne is offline Member
    Join Date
    Sep 2011
    Location
    Honolulu, Hawaii
    Posts
    9
    Rep Power
    0

    Default Re: Did I do this right? (Interest calculator)

    Yeah, sorry, didn't know there were rules. I explained it over there too, but with me being in Hawaii and working a swing schedule, and most everybody else being 3-6 hours ahead working during the day, I don't really get to have a constant flow of conversation. I only really get to post once before work and then hopefully I'll have an answer when I get back. I'll definitely make sure to put that at the beginning of my posts from now on though.

    "Resolved" my post on the other forum so no one else will bother with it.
    Last edited by r0sedeanne; 09-13-2011 at 10:37 AM.

Similar Threads

  1. Adding Interest to a Bank Account
    By petur170 in forum New To Java
    Replies: 20
    Last Post: 05-26-2011, 02:28 AM
  2. Calculate Interest
    By hacikho in forum New To Java
    Replies: 4
    Last Post: 10-09-2010, 04:15 AM
  3. Calculating compound interest using ONLY integers.
    By AprilFlowers in forum New To Java
    Replies: 2
    Last Post: 10-21-2009, 06:47 AM
  4. Help with compound interest program pleaze
    By Smirre in forum New To Java
    Replies: 1
    Last Post: 01-19-2009, 09:31 PM
  5. HELP FAST!!----Interest Calculator
    By Coop33 in forum New To Java
    Replies: 8
    Last Post: 10-15-2008, 01:44 PM

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
  •