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 7 of 7

Thread: Help please!

  1. #1
    Junior Member
    Join Date
    Mar 2011
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Help please!

    Hi guys,

    I need some help with this code, I just can't get it to run.

    Thanks!
    import java.util.Scanner;
          public class Invest{
                public static void main(String[] args){
          Scanner reader = new Scanner(System.in);
     
    	 double principal = 0;
     
    	 double rate = 0;
    	 int years = 0;
     
    	 double total = 0;
     
    	 System.out.println ("Enter your initial investment amount: ");
    	  principal = reader.nextInt();
     
     
         System.out.println ("Enter the number of years: ");
    	 rate = reader.nextInt();
     
     
    	 System.out.println ("Enter the interst rate: ");
    	 rate = reader.nextInt();
     
     
    	   total= (rate*years)*principal;
     
     
    	   {
     
     
     
    	      //System.out.println(" years");
     
    	     }
    	     System.out.print("your total investment is:" + total);
    	   }
     
    }
    Last edited by cindisue; March 28th, 2011 at 08:35 AM.

  2. #2
    Member vanDarg's Avatar
    Join Date
    Jan 2011
    Location
    Chicago
    Posts
    65
    My Mood
    Mellow
    Thanks
    1
    Thanked 7 Times in 7 Posts

    Default Re: Help please!

    For starters, principal, rate, and total are all of type double. Either change the types to int, or use the method nextDouble() rather than nextInt(). You may also want to tell us why it won't run. Will it compile, what error messages?
    "Everything should be made as simple as possible, but not simpler."
    Asking Questions for Dummies | The Java Tutorials | Java Coding Styling Guide

  3. The Following User Says Thank You to vanDarg For This Useful Post:

    cindisue (March 30th, 2011)

  4. #3
    Junior Member
    Join Date
    Mar 2011
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Help please!

    thank you, it was compiling but then giving me an error in the command line when I ran it, that was it and and I will need to work out the math

  5. #4
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: Help please!

    I compiled your code and it ran fine. Did you recently update it? If this thread now solved?
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  6. #5
    Junior Member
    Join Date
    Mar 2011
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Help please!

    I have changed the code and it worked in that version, however, my classmate told me that was not the correct output so I am trying to use string format and for some reason it will compile and take input but not display the table in the command line.

     
    import java.io.*;
    import java.util.Scanner;
          public class Invest{
                public static void main(String[] args){
          Scanner reader = new Scanner(System.in);
     
     
    	 double principal = 0;
    	 double rate = 0;
    	 int years = 0;
    	 double total = 0;
    	 int yearCount = 1;
    	 double gain = 0;
     
    	 System.out.println ("Enter your initial investment amount: ");
    	  principal = reader.nextDouble();
     
     
         System.out.println ("Enter the number of years: ");
    	 years = reader.nextInt();
     
     
    	 System.out.println ("Enter the interst rate: ");
    	 rate = reader.nextDouble();
     
         years = reader.nextInt();
         System.out.printf("%4s%8s%10s%12s%n", "Years", "Principal", "Total", "Gain" );
         while (yearCount <= years)
         {
    		 gain = principal * (1 + rate / 400);
    		 total = principal + gain;
    		 System.out.printf("%4d%8f%10f%12f%n",years, principal, total, gain);
    	   total= (rate*years)*principal;
    	   yearCount++;
    	   }
    	   {
    	   	     System.out.print("your total investment is:" + total);
       }
       }
    }

  7. #6
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Help please!

    1. Why are you taking years as input twice?
    2. There are some problems with your formatting size and type.
    3. You must look it more carefully.

  8. The Following User Says Thank You to Mr.777 For This Useful Post:

    cindisue (March 30th, 2011)

  9. #7
    Junior Member
    Join Date
    Mar 2011
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Help please!

    The 2nd years was commented out and I forgot to delete it before I pasted the code, but I changed the format and am now getting a display thanks!