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

Thread: Hard time with Strings

  1. #1
    Junior Member
    Join Date
    Oct 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Post Hard time with Strings

    I'm taking a Java class and we have a assignment from the book working with strings and nothing I do seems to work, here's my code, copied from the book and still receive errors.
    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, );
    }
    }
    Help me Please!!
    Last edited by Aboyordie; October 4th, 2012 at 08:14 PM.


  2. #2
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default Re: Hard time with Strings

    Quote Originally Posted by Aboyordie View Post
    ...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 ...
    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
    Last edited by Zaphod_b; October 4th, 2012 at 08:03 PM.

  3. #3
    Junior Member
    Join Date
    Oct 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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
    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, );
                                        ^

  4. #4
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default 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.

  5. #5
    Junior Member
    Join Date
    Oct 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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.
    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 -

    dialogexample.java:23: illegal start of expression
    JOptionPane.showMessageDialog(null,);



    When I put output after the null, I get 12 errors.

    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

  6. #6
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default Re: Hard time with Strings

    Quote Originally Posted by Aboyordie View Post
    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"
    Last edited by Zaphod_b; October 4th, 2012 at 10:31 PM.

  7. #7
    Junior Member
    Join Date
    Oct 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Hard time with Strings

    Im using TextWrangler and using terminal to compile and run do you think that may be causing the problem?

  8. #8
    Junior Member
    Join Date
    Oct 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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...

Similar Threads

  1. Having a hard time to solve this (probably easy for the rest of you)
    By obie in forum What's Wrong With My Code?
    Replies: 3
    Last Post: September 6th, 2012, 09:54 AM
  2. Singletons - I'm having a hard time understand how to implement them.
    By Illanair in forum Object Oriented Programming
    Replies: 6
    Last Post: February 16th, 2012, 05:49 PM
  3. I am having a hard time with my code
    By SandeeBee in forum What's Wrong With My Code?
    Replies: 14
    Last Post: November 12th, 2011, 09:34 AM
  4. I am having a hard time fixing this error
    By KuruptingYou in forum What's Wrong With My Code?
    Replies: 7
    Last Post: August 28th, 2011, 11:12 PM
  5. new to java.... having a hard time trying to read code
    By Newbie_96 in forum Java Theory & Questions
    Replies: 2
    Last Post: August 6th, 2011, 02:51 AM