Re: Hard time with Strings
Quote:
Originally Posted by
Aboyordie
...copied from the book...
There are a number of invalid characters.
How did you "copy" it? Typed it in? What text editor? Copied from screen rendering of pdf file? Or what?
Maybe something like the following ...
Code java:
import javax.swing.JOptionPane;
public class DialogExample {
public static void main(String[] args) {
String annualInterestRateString = JOptionPane.showInputDialog( "Enter yearly interest rate, for example 8.25:");
double annualInterestRate = Double.parseDouble(annualInterestRateString);
double monthlyInterestRate = annualInterestRate / 1200;
String numberOfYearsString = JOptionPane.showInputDialog( "Enter number of years as an integer, \nfor example 5:");
int numberOfYears = Integer.parseInt(numberOfYearsString);
String loanString = JOptionPane.showInputDialog( "Enter loan amount, for example 120000.95:");
double loanAmount = Double.parseDouble(loanString);
double monthlyPayment = loanAmount * monthlyInterestRate / (1 - 1 / Math.pow(1 + monthlyInterestRate, numberOfYears * 12));
double totalPayment = monthlyPayment * numberOfYears * 12;
monthlyPayment = (int)(monthlyPayment * 100) / 100.0;
totalPayment= (int)(totalPayment * 100) / 100.0;
String output = "The monthly payment is " + monthlyPayment + "\nThe total payment is " + totalPayment;
JOptionPane.showMessageDialog(null,output);
}
}
Since I have no idea what book you "copied" it from, I can't speak to accuracy, but maybe it is "close enough" to get you started.
I don't mind helping once, but next time you are on your own...
Cheers!
Z
Re: Hard time with Strings
Thanks for the response, I still got 10 errors though.
Im using the book, Introduction to Java Programing 8th ed. by Y. Liang, it was Required for my java class.
I typed it in my hand and i'm positive everything is exactly how its showed.
Im using TextWrangler as well.
When I fix one I get even more errors, this book is driving me crazy.
The errors I got
Code :
dialogexample.java:16: illegal character: \8218
double monthlyPayment = loanAmount * monthlyInterestRate / (1 – 1 / Math.pow(1 + monthlyInterestRate, numberOfYears * 12));
^
dialogexample.java:16: not a statement
double monthlyPayment = loanAmount * monthlyInterestRate / (1 – 1 / Math.pow(1 + monthlyInterestRate, numberOfYears * 12));
^
dialogexample.java:16: ';' expected
double monthlyPayment = loanAmount * monthlyInterestRate / (1 – 1 / Math.pow(1 + monthlyInterestRate, numberOfYears * 12));
^
dialogexample.java:16: ';' expected
double monthlyPayment = loanAmount * monthlyInterestRate / (1 – 1 / Math.pow(1 + monthlyInterestRate, numberOfYears * 12));
^
dialogexample.java:21: ';' expected
"\nThe total payment is " + totalPayment;
^
dialogexample.java:21: not a statement
"\nThe total payment is " + totalPayment;
^
dialogexample.java:22: illegal start of expression
JOptionPane.showMessageDialog(null, );
^
Re: Hard time with Strings
In order to keep up to date, each time you post, please include a new copy of the code. You make changes we can not see, and it gets hard to guess what your code looks like unless you post it again.
Also keep this little rule in mind. I say rule, it is my rule... Your code never has more than one error at a time. Period.
I don't care if you purposely go through the code and create 2000 errors. It has exactly one error at a time. Take the first listed problem as the only problem and attempt to fix it. The reason why is, the compiler can see things differently due to some errors. From this twisted point of view, many things can seem wrong. But fixing that one little ; or }, for example, can suddenly make many errors vanish.
All of that ';' expected, and illegal start of expression are some of the errors that can exist because another error made things look different.
Post the newest version of the code, any error messages you have, and your question.
Re: Hard time with Strings
I appreciate all the help and I followed your advice and fixed the first error first and I manged to get it down to one. But now im stuck again.
Code :
import javax.swing.JOptionPane;
public class DialogExample {
public static void main(String[] args) {
String annualInterestRateString = JOptionPane.showInputDialog(
"Enter yearly interest rate, for example 8.25:");
double annualInterestRate =
Double.parseDouble(annualInterestRateString);
double monthlyInterestRate = annualInterestRate / 1200;
String numberOfYearsString = JOptionPane.showInputDialog(
"Enter number of years as an integer, \nfor example 5:");
int numberOfYears = Integer.parseInt(numberOfYearsString);
String loanString = JOptionPane.showInputDialog( "Enter loan amount, for example 120000.95:");
double loanAmount = Double.parseDouble(loanString);
double monthlyPayment = loanAmount * monthlyInterestRate / (1 - 1 / Math.pow(1 +
monthlyInterestRate, numberOfYears * 12));
double totalPayment = monthlyPayment * numberOfYears * 12;
monthlyPayment = (int)( monthlyPayment * 100) / 100.0;
totalPayment = (int)( totalPayment * 100) / 100.0;
String output = "The monthly payment is " + monthlyPayment + "\nThe total payment is " +
totalPayment;
JOptionPane.showMessageDialog(null,);
}
}
The weird thing is it gives me one error -
Code :
dialogexample.java:23: illegal start of expression
JOptionPane.showMessageDialog(null,);
When I put output after the null, I get 12 errors.
Code :
dialogexample.java:5: incompatible types
found : java.lang.String
required: String
String annualInterestRateString = JOptionPane.showInputDialog(
^
dialogexample.java:8: parseDouble(java.lang.String) in java.lang.Double cannot be applied to (String)
Double.parseDouble(annualInterestRateString);
^
dialogexample.java:10: incompatible types
found : java.lang.String
required: String
String numberOfYearsString = JOptionPane.showInputDialog(
^
dialogexample.java:13: cannot find symbol
symbol : method parseInt(String)
location: class java.lang.Integer
int numberOfYears = Integer.parseInt(numberOfYearsString);
^
dialogexample.java:14: incompatible types
found : java.lang.String
required: String
String loanString = JOptionPane.showInputDialog( "Enter loan amount, for example 120000.95:");
^
dialogexample.java:15: parseDouble(java.lang.String) in java.lang.Double cannot be applied to (String)
double loanAmount = Double.parseDouble(loanString);
^
dialogexample.java:18: cannot find symbol
symbol : class double
location: class DialogExample
double totalPayment = monthlyPayment * numberOfYears * 12;
^
dialogexample.java:20: cannot find symbol
symbol : variable totalPayment
location: class DialogExample
totalPayment = (int)( totalPayment * 100) / 100.0;
^
dialogexample.java:21: incompatible types
found : java.lang.String
required: String
String output = "The monthly payment is " + monthlyPayment + "\nThe total payment is " +
^
./String.java:7: cannot find symbol
symbol : method nextString()
location: class java.util.Scanner
String s1 = input.nextString();
^
./String.java:8: cannot find symbol
symbol : method nextString()
location: class java.util.Scanner
String s2 = input.nextString();
^
./String.java:9: cannot find symbol
symbol : method nextString()
location: class java.util.Scanner
String s3 = input.nextString();
^
12 errors
Re: Hard time with Strings
Quote:
Originally Posted by
Aboyordie
Thanks for the response, I still got 10 errors though....
I am working on a different computer than the one that I prevoiusly used when I posted my response.
I copied the text from my previous post into my text editor. I compiled and executed from a command line. The program ran to completion. (I had also compiled on the other computer before posting.)
Asked for yearly interest rate.
Asked for number of years.
Asked for loan amount.
Calculated and displayed Monthly Payment and Total Payment.
I won't say whether I think the answers are correct. I just wanted you to know that code that I posted had no compiler or run-time errors.
I have no knowledge of or interest in TextWrangler, whatever that is, but I'm thinking you have some kind of locale or other configuration issues that put Unicode (or something) characters in the code instead of ASCII.
My setup: javac and java version 1.6.0_22 on Centos 5.8 Linux. For simple projects like this I use gvim as the text editor and compile and execute from a command line.
I'm done here.
Cheers!
Z
"Over and out"
Re: Hard time with Strings
Im using TextWrangler and using terminal to compile and run do you think that may be causing the problem?
Re: Hard time with Strings
I guess textwrangler and terminal is the problem it works fine on netbeans that so strange; teachers dumb for making us use the terminal method...