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: My code is having error, may be a lost of precision

  1. #1
    Junior Member
    Join Date
    Jan 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default My code is having error, may be a lost of precision

    import java.util.*;
    	import java.text.*;
     
    	public class Interest 
     
    	{
        static Scanner input = new Scanner(System.in).useDelimiter("\r\n");
    	static DecimalFormat fmt = new DecimalFormat("0.00");
     
     
     
     
        public static void main(String[] args) throws Exception
        {
        	int choice=0;
        	int i=0;
        	double [] iPrinciple = new double[4];
    	    double [] iEarned = new double [4];
    	    double [] iRate = new double [4];
    	    int [] iDays = new int [4];
     
     
        	while (choice !='4')
        	{
        		System.out.println("      Interest Calculator     ");
        		System.out.println("================================");
        		System.out.println("1)Calculate the interest payable");
        		System.out.println("2)Calculate the principle required to earn $x of interest");
        		System.out.println("3)Calculate the time required to earn $x of interest");
        		System.out.println("4)Exit the program");
        		System.out.println("");
        		System.out.print("Please select your choice (1-4): ");
        		choice = input.nextInt();
        		switch(choice)		
        	{
        		case 1 : 	for (i=0; i<10; i++)
        				  	{
        				  		System.out.print("Enter principle amount $: ");
        				  		iPrinciple[i] = input.nextDouble();
        				  		System.out.print("Enter interest rate in %: ");
        				  		iRate[i] = input.nextDouble();
        				  		System.out.print("Enter period rate(time) in days: ");
        				  		iDays[i] = input.nextInt();
        				  		iEarned[i] = (iPrinciple[i]*(iRate[i]/100)*(iDays[i]/360));
        				  		System.out.println(" Interest earned for principle of " + iPrinciple[i] + " at interest rate of " + iRate[i] + " is " + fmt.format(iEarned[i]));
    							break;
        					}
     
        		case 2: for (i=0; i<10; i++)
        				  {
        				  	System.out.print("Enter the interest amount to be earned in $ : ");
        				  	iEarned[i] = input.nextDouble();
        				  	System.out.print("Enter the  interest rate in % : ");
        				  	iRate[i] = input.nextDouble();
        				  	System.out.print("Enter the period<time> in days : ");
        				  	iDays[i] = input.nextInt();
        				  	iPrinciple[i] = (iEarned[i]/((iRate[i]/100)*(iDays[i]/360)));
        				  	System.out.println(" To earn an interest of " + iEarned[i] + " over a period of " + iDays[i] + " you need a principle of " + fmt.format(iPrinciple[i]));
        				  }
        				  break;
        	  	case 3: for (i=0; i<10; i++)
        				  {
        				  	System.out.println("Enter the interest amount to be earned in $ : ");
        				  	iEarned[i] = input.nextDouble();
        				  	System.out.println("Enter the interest rate in % : ");
        				  	iRate[i] = input.nextDouble();
        				  	System.out.println("Enter the principle amount in $: ");
        				  	iDays[i] = input.nextInt();
        				  	iPrinciple[i] = iEarned[i]/(iRate[i]*iDays[i]);
        				  	System.out.println(" To earn an interest of " + iEarned[i] + " over a period of " + iDays[i] + " you need a principle of " + fmt.format(iPrinciple[i]));
     
        				 }
        				 break;
     
        		case 4: for (int j=0; j<i ; j++)
        				{
        					System.out.println("Principle        Rate      Days        Interest                  Total");
        					System.out.println("  ($)             (%)                    ($)                     ($)");
        					System.out.println("---------        ----      ----        --------                  -----");
        					System.out.println(iPrinciple[i] +  iRate[i] + iDays[i] +  iEarned[i]  +    (iPrinciple[i] + iEarned[i]));
     
        				}
        				break;
        		default: System.out.println("This is an invalid choice, please select 1-4 only ");
        	}
     
        	System.out.println("");	
     
    }    
    }
    }
    Please i need help in this . This doesnt come out as expected from the code i input in. And it doesnt calculate.
    Last edited by copeg; January 30th, 2011 at 09:51 PM.


  2. #2
    Member DanBrown's Avatar
    Join Date
    Jan 2011
    Posts
    134
    My Mood
    Confused
    Thanks
    1
    Thanked 12 Times in 12 Posts

    Default Re: My code is having error, may be a lost of precision

    case 1 : for (i=0; i<10; i++)
    {
    System.out.print("Enter principle amount $: ");
    iPrinciple[i] = input.nextDouble();
    System.out.print("Enter interest rate in %: ");
    iRate[i] = input.nextDouble();
    System.out.print("Enter period rate(time) in days: ");
    iDays[i] = input.nextInt();
    iEarned[i] = (iPrinciple[i]*(iRate[i]/100)*(iDays[i]/360));
    System.out.println(" Interest earned for principle of " + iPrinciple[i] + " at interest rate of " + iRate[i] + " is " + fmt.format(iEarned[i]));
    break;
    }
    why are you using loop in every case
    Thanks and Regards
    Dan Brown

    Common Java Mistakes

Similar Threads

  1. Simple code error
    By robin28 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: December 7th, 2010, 03:25 PM
  2. [SOLVED] Help with an error in code
    By cis170 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: December 6th, 2010, 10:21 AM
  3. Error with code
    By JJTierney in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 4th, 2010, 05:23 PM
  4. [SOLVED] Loss of Precision (Double/Int)
    By Scotty in forum What's Wrong With My Code?
    Replies: 6
    Last Post: October 9th, 2010, 01:45 PM
  5. [SOLVED] "possible loss of precision", except not, code doesn't work, simple question
    By Perd1t1on in forum What's Wrong With My Code?
    Replies: 2
    Last Post: July 24th, 2010, 07:11 PM