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

Thread: Java Method Help

  1. #1
    Member
    Join Date
    Feb 2013
    Posts
    30
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Java Method Help

    I'm trying to create a method for the display interest and I'm struggling with how to come up with how to get it to work. Any help would be great!

     import java.util.Scanner;
    public class InterestCalculator4 {
    	public static void main(String[] args )
    	{
    		Scanner input = new Scanner(System.in);
     
    		String strAnsYesNo;
    		char ansYesNo = 0;
     
    		do{
     
    			//Prompt user to enter loan amount
    			System.out.print("Enter Loan Amount: ");
    		    double principle = input.nextInt();
     
    		    //Prompt user to enter interest rate
    			System.out.print("Enter Yearly Interest Rate (1 to 100 percent): ");
    		    double interest = input.nextDouble(); 
     
    		  //Prompt user to enter term 
    		    System.out.print("Enter the Term (in months): ");
    		    double term = input.nextInt(); 
     
    		   if (principle > 0) 
    			   {
    			    	if (interest >= 0 && interest <= 100) 
    			    	{
    			    		if(term > 0)
    			    		{
    			    			int type = 0;	 
     
    			    			    //Prompt user to enter calculation type
    			    			   do{System.out.print("Enter Interest Calculation Type (1  Simple, 2  Monthly Compounded, 3  Daily Compounded):");
    			    			    type = input.nextInt();
     
     
    			    			   switch(type) 
    			    			    {
    			    			    case 1:
    			    			    break;
     
    			    			    case 2:
    			    			    break;
     
    			    			    case 3:
    			    			    break;
     
    			    			    default: System.out.println("Calculation Type Error: You must select 1, 2 or 3. You entered " +type);
    			    			    break;
     
    			    		        }
    			    			   }while(type<1||type>3);
    			    			}
    			    	    else
    			    	    {
    			    	    	System.out.println("Data Error: Term must be greater than zero. You entered " +term);
     
    			    	    }
    			        }
    			    	else 
    			    	{
    			    		System.out.println("Data Error: Interest rate must be greater than or equal to zero and less than or equal to 100. You entered " +interest);
    			    	}
    			   }
    			   else
    			   {	
    			    		System.out.println("Data Error: Loan amount must be greater than zero. You entered " +principle);
    			   }
     
    		// Clear out buffer
    		   input.nextLine();
     
    		do{
    			System.out.println("Calculate another loan interest (Yes/No)?");
    			strAnsYesNo = input.nextLine();
     
    			// Make sure user entered in data
    			if (strAnsYesNo.length() > 0)
    			ansYesNo = strAnsYesNo.charAt(0);
     
    			if(ansYesNo!='y'&& ansYesNo!='Y'&& ansYesNo!='n'&& ansYesNo!='n')
    			{
    				System.out.println("Please enter 'Yes' or 'No'?");
    			}
     
    		}while(ansYesNo!='y'&& ansYesNo!='Y'&& ansYesNo!='n'&& ansYesNo!='n');
     
     
    		}while (ansYesNo =='y' || ansYesNo =='Y');
    		System.out.println("Thank you for using Interest Calculator!");
    	}
     
     
    	/** Round **/
    	  public static double round(double numb1, double numb2) {
    	    double round = ((double) Math.round(numb1*(Math.pow(10, numb2)))/(Math.pow(10, numb2)));;
    		return round;
    	  }
     
    	/** Calculate Simple **/
    	  public static double calculateSimpleInterest(double numb1, double numb2, double numb3) {
    	    double calculateSimpleInterest = ((numb1)*(numb2/100.0)*(numb3/12.0));
    		return calculateSimpleInterest;
    	  }
     
    	  /** Calculate Compounded Daily **/
    	  public static double calculateCompoundInterest(double numb1, double numb2, double numb3, double numb4 ) {
    	     double calculateCompoundInterest = (numb1*Math.pow((1.0+((numb2/100.0)/numb4)),(numb4*(numb3/12.0))))-numb1;
    		return calculateCompoundInterest;
    	  }
     
    	  /** display interest **/
    	  public static char displayInterest(double numb1, double numb2, double numb3, char word1, double numb4, double numb5 ) {
    		  numb5 = numb1 + numb4; 
     
    		  System.out.println("Principal Amount    Interest Rate    Term    Calculation Type    Calculated Interest    Total Repayment");
    		  System.out.println("----------------    -------------    ----    ----------------    -------------------    ---------------");
    		  System.out.println("+numb1",              "+numb2",       "numb3", "+word1",           "+numb4",                "+numb5");
    	  }
    	}


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,321
    Thanks
    56
    Thanked 2,374 Times in 2,345 Posts

    Default Re: Java Method Help

    how to come up with how to get it to work
    Can you describe what the method is supposed to do?
    What happens now when you compile and execute the code?
    Copy the output and paste it here and explain what is wrong with it.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Feb 2013
    Posts
    30
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java Method Help

    Sorry!
    Purpose: To display the result of the calculated interest to the user. Parameters: You will need to determine the number of parameters and their appropriate data type of each parameter to display the output to the user.

    It is suppose to print out
    Principal Amount Interest Rate Term Calculation Type Calculated Interest Total Repayment Amount
    ---------------- ------------- ---- ---------------- ------------------- ----------------------
    $1375.0 1.175 7 Compound Monthly $9.45221 $1384.45

    I was thinking with the parameters
    numb1= principle
    numb2= interest
    numb3=term
    word1= the type of interest calculated
    numb4= the interest
    numb5= the total repayment(interest+principle)

    --- Update ---

    When the user enters in the principle, interest, and term. Then the user enters in which interest type they'd like to calc so 1-simple 2-monthly compounded 3-daily compounded and when they enter 1,2, or 3 it prints out numbers will vary though to which the user enters

    Principal Amount Interest Rate Term Calculation Type Calculated Interest Total Repayment Amount
    ---------------- ------------- ---- ---------------- ------------------- ----------------------
    $1375.0 1.175 7 Compound Monthly $9.45221 $1384.45

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,321
    Thanks
    56
    Thanked 2,374 Times in 2,345 Posts

    Default Re: Java Method Help

    What happens now when you compile and execute the code?
    Copy the output and paste it here and explain what is wrong with it.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Feb 2013
    Posts
    30
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java Method Help

    It won't compile it gives me an error at
    System.out.println("+numb1",              "+numb2",       "numb3", "+word1",           "+numb4",                "+numb5");
    saying the method println(string) in the type printstream is not applicable for the arguments ( string, string, string, string, string)

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,321
    Thanks
    56
    Thanked 2,374 Times in 2,345 Posts

    Default Re: Java Method Help

    the method println(string)
    That says the println() method takes ONE String argument. The posted code has several String arguments separated by commas. Try removing the commas and making the arg one String by using the + operator to concatenate all the Strings into one String.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Member
    Join Date
    Feb 2013
    Posts
    30
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java Method Help

    I changed it to
     /** display interest **/
    	  public static void displayInterest(double numb1, double numb2, double numb3, char word1, double numb4, double numb5 ) {
    		  numb5 = numb1 + numb4; 
     
    		System.out.println("Principal Amount    Interest Rate    Term    Calculation Type    Calculated Interest    Total Repayment");
    		  System.out.println("----------------    -------------    ----    ----------------    -------------------    ---------------");
    		 System.out.println("+numb1              +numb2       +numb3 +word1           +numb4                +numb5");
    	  }

    Then when I put it to where I want to print out when they select 1,2, or 3 it has an error
     switch(type) 
    			    			    {
    			    			    case 1: displayInterest(principle,interest,term,"Simple",principle);
    			    			    break;
     
    			    			    case 2: displayInterest(principle,interest,term,"Monthly Compounded",principle);
    			    			    break;
     
    			    			    case 3:displayInterest(principle,interest,term,"Daily Compounded",principle);
    			    			    break;
     
    			    			    default: System.out.println("Calculation Type Error: You must select 1, 2 or 3. You entered " +type);
    			    			    break;
     
    			    		        }

    It has an error saying it's arguments are not applicable

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,321
    Thanks
    56
    Thanked 2,374 Times in 2,345 Posts

    Default Re: Java Method Help

    Please copy the full text of the error messages and paste it here. There is useful and important info in the messages.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Member
    Join Date
    Feb 2013
    Posts
    30
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java Method Help

    Sorry, it won't compile but the error is the method displayInterest(double,double, double, char,double) in the type InterestCalculator4 is not applicable for the arguments (double, double,double, string, double)

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,321
    Thanks
    56
    Thanked 2,374 Times in 2,345 Posts

    Default Re: Java Method Help

    it won't compile
    The compiler prints out error messages when it finds errors.
    Here is a sample error message from the javac compiler:
    TestSorts.java:138: cannot find symbol
    symbol  : variable var
    location: class TestSorts
             var = 2;
             ^
    It gives the source line number, the line with the error and describes the error.

    the method displayInterest(double,double, double, char,double) in the type InterestCalculator4
    is not applicable for the arguments (double, double,double, string, double)
    Did you compare those two argument lists and see where they are different? You need to change one of them so that they match.
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Member
    Join Date
    Feb 2013
    Posts
    30
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java Method Help

     Exception in thread "main" java.lang.Error: Unresolved compilation problems: 
    	a cannot be resolved to a variable
    	Monthly cannot be resolved to a variable
    	Syntax error on token "Compounded", delete this token
    	The method displayInterest(double, double, double, char, double, double) in the type InterestCalculator4 is not applicable for the arguments (double, double, double, String, double)

    at InterestCalculator4.main(InterestCalculator4.java: 45)

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,321
    Thanks
    56
    Thanked 2,374 Times in 2,345 Posts

    Default Re: Java Method Help

    Exception in thread "main" java.lang.Error: Unresolved compilation problems:
    You should compile the code and fix the problems before trying to execute it. That error message is from when you try to execute the code. It won't execute with compiler errors!!!

    Is this the error: Monthly cannot be resolved to a variable
    Does the code use a variable named Monthly that is not defined?

    See the end of post#10 for the other part.
    If you don't understand my answer, don't ignore it, ask a question.

  13. #13
    Member
    Join Date
    Feb 2013
    Posts
    30
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java Method Help

    Would I define it was a char or string? That's what I'm confused about

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,321
    Thanks
    56
    Thanked 2,374 Times in 2,345 Posts

    Default Re: Java Method Help

    A char is one letter, a String allows for many characters. Which do you need? A String can be a single letter.
    A char can never be more than one letter.
    If you don't understand my answer, don't ignore it, ask a question.

  15. #15
    Member
    Join Date
    Feb 2013
    Posts
    30
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java Method Help

    When I type in String it doesn't recognize it as a string variable.

  16. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,321
    Thanks
    56
    Thanked 2,374 Times in 2,345 Posts

    Default Re: Java Method Help

    it doesn't recognize it as a string variable.
    Please copy the full text of the error message and paste it here.
    If you don't understand my answer, don't ignore it, ask a question.

  17. #17
    Member
    Join Date
    Feb 2013
    Posts
    30
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java Method Help

    I got it to work! Thanks for your help and baring with me!

  18. #18
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,321
    Thanks
    56
    Thanked 2,374 Times in 2,345 Posts

    Default Re: Java Method Help

    Glad you got it to work.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Java main method
    By JEV23 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 13th, 2013, 08:41 AM
  2. [SOLVED] Java Static Method
    By maple1100 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: January 3rd, 2013, 09:09 PM
  3. [SOLVED] How to create a Java generic method, similar to a C++ template method?
    By Sharmeen in forum Object Oriented Programming
    Replies: 3
    Last Post: October 18th, 2012, 03:33 AM
  4. Use a Method in Java to find the Last name of any given name
    By finjws in forum Collections and Generics
    Replies: 5
    Last Post: February 20th, 2012, 04:33 AM
  5. How to display a Java method using Java Swing
    By IHeartProgramming in forum What's Wrong With My Code?
    Replies: 14
    Last Post: May 18th, 2011, 02:08 PM