10 Replies - 1328 Views - Last Post: 25 January 2010 - 03:24 PM Rate Topic: -----

#1 sharkfin31   User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 9
  • Joined: 23-January 10

I have an error in my code

Post icon  Posted 23 January 2010 - 06:33 PM

Hello everyone,

I am sure you are tired of students asking questions but I need some help with two errors in my code. Just looking for some suggestions. Here is my code. The lines in red are the errors.

public class wk1calculator //file name is wk1calculator.java{
public static void main(String[] args) { //public class, no user input expected
String formattedLoanPrincipal = "Loan Amount: $200,000";
//intialize variables needed for the monthly payment calculation
double loanPrincipal = 200000; //Loan pricipal ammount.
double yearlyInterest = 5.75; //yearly interest rate.
int loanTerm = 30; //Loan length in years. Used int to remove decimal point after 30 in output

//Set the variable for the monthly payment
double monthlyPayment = 0; //Sets variable to zero.
String correctedMonthlyPayment = new String();

//Calculate the monthly interest on the loan
double MonthlyInterest = 0;
String monthlyInterest = new String();
MonthlyInterest = yearlyInterest /(12 * 100);

//Calculate the number of months over the life of the loan
double TermInMonths = 0;
String termInMonths = new String();
TermInMonths = loanTerm * 12;

//Final calculation of the loan payment
monthlyPayment = (loanPrincipal * MonthlyInterest) / (1.0-(Math.pow((1.0 + MonthlyInterest),-TermInMonths)));
correctedMonthlyPayment = "Rounded Payment: $" + String.valueOf(Math.rint(monthlyPayment*100)/100);

//output printed to the screen
System.out.println ("Written by Matthew Keenan"); //written by
System.out.println ("Week 1 PRG420 assignment"); //assignment and class
System.out.println ("Principal loan ammount is: $" + formattedLoanPrincipal); //displays the loan principal
System.out.println ("Loan interest rate is: " + yearlyInterest + "%"); //displays the APR
System.out.println ("Loan term is: " + loanTerm + " years"); //displays the loan length
System.out.println ("Monthly payment : $" = String.valueof(MonthlyPayment));

Is This A Good Question/Topic? 0
  • +

Replies To: I have an error in my code

#2 macosxnerd101   User is online

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12605
  • View blog
  • Posts: 45,742
  • Joined: 27-December 08

Re: I have an error in my code

Posted 23 January 2010 - 06:42 PM

In the future, please :code:

For the first error, I'm betting it's b/c of the placement of the comment which comments out the opening brace. public class wk1calculator //file name is wk1calculator.java{. You should probably move the comment outside of the brace.

For the second error, it is a capitalization mistake. You need to lowercase the 'M' in MonthlyPayment to read monthlyPayment.
Was This Post Helpful? 0
  • +
  • -

#3 sharkfin31   User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 9
  • Joined: 23-January 10

Re: I have an error in my code

Posted 23 January 2010 - 07:04 PM

I did the change to second error but I get this error:

Unexpected type
required: variable
found: value

cannot find symbol
symbol: method valueof(double)
location: class java.lang.String

reached end of file while parsing

For the first error I don't understand what you're saying. I am using netbeans to find these errors.
Was This Post Helpful? 0
  • +
  • -

#4 Dogstopper   User is offline

  • The Ninjaducky
  • member icon

Reputation: 2972
  • View blog
  • Posts: 11,223
  • Joined: 15-July 08

Re: I have an error in my code

Posted 23 January 2010 - 07:22 PM

Yes, for EVERY opening brace '{', you need a corresponding closing brace '}' Also, ensure that no comments are preventing the braces from being seen as macosxnerd101 pointed out. Perhaps you should post your updated code.

:code:
Was This Post Helpful? 0
  • +
  • -

#5 sharkfin31   User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 9
  • Joined: 23-January 10

Re: I have an error in my code

Posted 23 January 2010 - 07:28 PM

Her you go

[color=#FF0000]public class wk1calculator //file name is wk1calculator.java{[/color]public static void main(String[] args) { //public class, no user input expected
String formattedLoanPrincipal = "Loan Amount: $200,000";
//intialize variables needed for the monthly payment calculation
double loanPrincipal = 200000; //Loan pricipal ammount. 
double yearlyInterest = 5.75; //yearly interest rate. 
int loanTerm = 30; //Loan length in years. Used int to remove decimal point after 30 in output

//Set the variable for the monthly payment
double monthlyPayment = 0; //Sets variable to zero.
String correctedMonthlyPayment = new String();

//Calculate the monthly interest on the loan
double MonthlyInterest = 0;
String monthlyInterest = new String();
MonthlyInterest = yearlyInterest /(12 * 100);

//Calculate the number of months over the life of the loan
double TermInMonths = 0;
String termInMonths = new String();
TermInMonths = loanTerm * 12;

//Final calculation of the loan payment
monthlyPayment = (loanPrincipal * MonthlyInterest) / (1.0-(Math.pow((1.0 + MonthlyInterest),-TermInMonths)));
correctedMonthlyPayment = "Rounded Payment: $" + String.valueOf(Math.rint(monthlyPayment*100)/100);

//output printed to the screen
System.out.println ("Written by Matthew Keenan"); //written by
System.out.println ("Week 1 PRG420 assignment"); //assignment and class
System.out.println ("Principal loan ammount is: $" + formattedLoanPrincipal); //displays the loan principal
System.out.println ("Loan interest rate is: " + yearlyInterest + "%"); //displays the APR
System.out.println ("Loan term is: " + loanTerm + " years"); //displays the loan length
[color=#FF0000]System.out.println ("monthly payment : $" = String.valueof(monthlyPayment)); [/color] 

Was This Post Helpful? 0
  • +
  • -

#6 macosxnerd101   User is online

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12605
  • View blog
  • Posts: 45,742
  • Joined: 27-December 08

Re: I have an error in my code

Posted 23 January 2010 - 08:48 PM

BBCode is not parsed within code tags. Could you edit your code to remove any BBCode so that we can read it better? Thanks for helping us help you! :)
Was This Post Helpful? 0
  • +
  • -

#7 Dogstopper   User is offline

  • The Ninjaducky
  • member icon

Reputation: 2972
  • View blog
  • Posts: 11,223
  • Joined: 15-July 08

Re: I have an error in my code

Posted 23 January 2010 - 09:15 PM

public class wk1calculator //file name is wk1calculator.java{public static void main(String[] args) { 
YOU COMPLETELY IGNORED MACOSXNERD101 That '{' has to be BEFORE the comment....

	String formattedLoanPrincipal = "Loan Amount: $200,000";
	//intialize variables needed for the monthly payment calculation
	double loanPrincipal = 200000; //Loan pricipal ammount. 
	double yearlyInterest = 5.75; //yearly interest rate. 
	int loanTerm = 30; //Loan length in years. Used int to remove decimal point after 30 in output

	 //Set the variable for the monthly payment
	double monthlyPayment = 0; //Sets variable to zero.
	String correctedMonthlyPayment = new String();

	 //Calculate the monthly interest on the loan
	double MonthlyInterest = 0;
	String monthlyInterest = new String();
	MonthlyInterest = yearlyInterest /(12 * 100);

	//Calculate the number of months over the life of the loan
	double TermInMonths = 0;
	String termInMonths = new String();
	TermInMonths = loanTerm * 12;

	//Final calculation of the loan payment
	monthlyPayment = (loanPrincipal * MonthlyInterest) / (1.0-(Math.pow((1.0 + MonthlyInterest),-TermInMonths)));
	correctedMonthlyPayment = "Rounded Payment: $" + String.valueOf(Math.rint(monthlyPayment*100)/100);

	//output printed to the screen
	System.out.println ("Written by Matthew Keenan"); //written by
	System.out.println ("Week 1 PRG420 assignment"); //assignment and class
	System.out.println ("Principal loan ammount is: $" + formattedLoanPrincipal); //displays the loan principal
	System.out.println ("Loan interest rate is: " + yearlyInterest + "%"); //displays the APR
	System.out.println ("Loan term is: " + loanTerm + " years"); //displays the loan length
	System.out.println ("monthly payment : $" = String.valueof(monthlyPayment));



You have many problems in here...EVERY opening brace, { that you use MUST have a corresponding closing brace }

Also, all running code MUST be inside methods, and in your case, most likely main...See look at th following structure:
public class wk1calculator {
	public static void main(String[] args) {
		//... Your logic should go HERE
	} // CLOSE THE METHOD
} // Closing brace for the class



In the part that says logic goes here, put your code...
Was This Post Helpful? 0
  • +
  • -

#8 erik.price   User is offline

  • D.I.C Lover
  • member icon

Reputation: 486
  • View blog
  • Posts: 2,690
  • Joined: 18-December 08

Re: I have an error in my code

Posted 23 January 2010 - 11:35 PM

A // is a line comment meaning, it will comment out all code until a \n (or \r\n for Windows) is seen.

You might want to use the block comment instead here /*A COMMENT!*/ int thisIsValid = 0;. That kind of comment only is effective from the /* to the */
Was This Post Helpful? 0
  • +
  • -

#9 sharkfin31   User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 9
  • Joined: 23-January 10

Re: I have an error in my code

Posted 25 January 2010 - 02:47 PM

Ok guys I ahve been going at it non stop and still have problems. My 3 errors are in the last line of the code according to my compiler. Any suggestions?

[code]

public class wk1calculator //file name is wk1calculator.java
{
public static void main(String[] args) { //public class, no user input expected
String formattedLoanPrincipal = "Loan Amount: $200,000";
//intialize variables needed for the monthly payment calculation
double loanPrincipal = 200000; //Loan pricipal ammount.
double yearlyInterest = 5.75; //yearly interest rate.
int loanTerm = 30; //Loan length in years. Used int to remove decimal point after 30 in output

//Set the variable for the monthly payment
double monthlyPayment = 0; //Sets variable to zero.
String correctedMonthlyPayment = new String();

//Calculate the monthly interest on the loan
double MonthlyInterest = 0;
String monthlyInterest = new String();
MonthlyInterest = yearlyInterest /(12 * 100);

//Calculate the number of months over the life of the loan
double TermInMonths = 0;
String termInMonths = new String();
TermInMonths = loanTerm * 12;

//Final calculation of the loan payment
monthlyPayment = (loanPrincipal * MonthlyInterest) / (1.0-(Math.pow((1.0 + MonthlyInterest),-TermInMonths)));
correctedMonthlyPayment = "Rounded Payment: $" + String.valueOf(Math.rint(monthlyPayment*100)/100);

//output printed to the screen
System.out.println ("Written by Matthew Keenan"); //written by
System.out.println ("Week 1 PRG420 assignment"); //assignment and class
System.out.println ("Principal loan ammount is: $" + formattedLoanPrincipal); //displays the loan principal
System.out.println ("Loan interest rate is: " + yearlyInterest + "%"); //displays the APR
System.out.println ("Loan term is: " + loanTerm + " years"); //displays the loan length
System.out.println ("monthlyPayment : $" + String.value of (monthlyPayment));

}

}

[\code]
Was This Post Helpful? 0
  • +
  • -

#10 erik.price   User is offline

  • D.I.C Lover
  • member icon

Reputation: 486
  • View blog
  • Posts: 2,690
  • Joined: 18-December 08

Re: I have an error in my code

Posted 25 January 2010 - 03:10 PM

Method names are always one word. Spaces are not allowed.
String.value of (monthlyPayment));


Use String.valueOf(monthlyPayment) instead
Was This Post Helpful? 0
  • +
  • -

#11 macosxnerd101   User is online

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12605
  • View blog
  • Posts: 45,742
  • Joined: 27-December 08

Re: I have an error in my code

Posted 25 January 2010 - 03:24 PM

I think you have a typo in your closing code tag. Recall that code tags look like this: :code:

Also, it would be more efficient to forgo the use of the String.valueOf() method in terms of memory. Since Strings are immutable (meaning you have to create a new String object in order to change content), so with the String.valueOf() method in your println() statement, you are looking at 3 String objects total. Below is the breakdown for System.out.println ("monthlyPayment : $" + String.valueOf(monthlyPayment));:
-1 String: "monthlyPayment: $"
-1 String: String.valueOf(monthlyPayment)
-1 String to concatenate the above two.

If you instead just use the + operator like so: System.out.println ("monthlyPayment : $" + monthlyPayment);, you are eliminating the String returned from String.valueOf(monthlyPayment) and going straight into the concatenation.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1