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: New to Programming

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

    Default New to Programming

    Hi, I'm taking a beginning programming class for Java and can't figure out what errors I made in my first program. This is supposed to be a very simple program and I know I probably look stupid getting confused on it, so please don't bother making fun of me.
    import java.util.*;
     
    public class proj1
    {
            public static void main(String[] args) 
            {	double monthlyPayment, annualInterest, 
    		       outstandingBalance, accountBalance, paidInterest,
                           paidPrincipal;
     
    		annualInterestRate = .0625;
    		paidInterest = (annualInterest / 12) * 
                                      outstandingBalance;
    		paidPrincipal = monthlyPayment - paidInterest;
    		accountBalance = outstandingBalance - paidPrincipal;
     
    		System.out.println("Enter this month's payment:");
     
    		Scanner keyboard = new Scanner(System.in);
    		monthlyPayment  = keyboard.nextDouble();
     
    		System.out.println("Enter the outstanding balance of the account:");
     
    		Scanner keyboard = new Scanner(System.in);
    		outstandingBalance = keyboard.nextDouble();
     
    		System.out.println("Here is a breakdown of the 
    payment:");
    		System.out.println("Interest paid-" paidInterest);
                    System.out.println("Amount applied to principal" 
    paidPrincipal);
    		System.out.println("After the payment, the account 
    balance is:" accountBalance);
    	}
    }

    when I tried compiling, this is what I got:

    javac proj1.java
    proj1.java:26: unclosed string literal
    System.out.println("Here is a breakdown of the
    ^
    proj1.java:26: ';' expected
    System.out.println("Here is a breakdown of the
    ^
    proj1.java:27: illegal start of expression
    payment:");
    ^
    proj1.java:27: unclosed string literal
    payment:");
    ^
    proj1.java:28: ')' expected
    System.out.println("Interest paid-" paidInterest);
    ^
    proj1.java:28: illegal start of expression
    System.out.println("Interest paid-" paidInterest);
    ^
    proj1.java:29: ')' expected
    System.out.println("Amount applied to principal"
    ^
    proj1.java:30: illegal start of expression
    paidPrincipal);
    ^
    proj1.java:31: unclosed string literal
    System.out.println("After the payment, the account
    ^
    proj1.java:31: ';' expected
    System.out.println("After the payment, the account
    ^
    proj1.java:32: unclosed string literal
    balance is:" accountBalance);
    ^
    11 errors


  2. #2
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: New to Programming

    Next time you post, please include tags around your code such as ones in my signature.
    I've reposted your code in a readable format:
    import java.util.*;
     
    public class proj1 {
     
        public static void main(String[] args) {
            double monthlyPayment, annualInterest,
                    outstandingBalance, accountBalance, paidInterest,
                    paidPrincipal;
     
            annualInterestRate = .0625;
            paidInterest = (annualInterest / 12) * outstandingBalance;
            paidPrincipal = monthlyPayment - paidInterest;
            accountBalance = outstandingBalance - paidPrincipal;
     
            System.out.println("Enter this month's payment:");
     
            Scanner keyboard = new Scanner(System.in);
            monthlyPayment = keyboard.nextDouble();
     
            System.out.println("Enter the outstanding balance of the account:");
     
            Scanner keyboard = new Scanner(System.in);
            outstandingBalance = keyboard.nextDouble();
     
            System.out.println("Here is a breakdown of the payment:");
            System.out.println("Interest paid-" paidInterest);
            System.out.println("Amount applied to principal" paidPrincipal);
            System.out.println("After the payment, the account balance is:" accountBalance);
        }
    }

    • The keyboard object is defined twice, you only need to define keyboard once, and reuse it when you need to.
    • In-order to print variables alongside Strings, you need to use the + operator to join them together.
    • You attempt to perform paidInterest = (annualInterest / 12) * outstandingBalance; before even initialising outstandingBalance.
    • annualInterestRate isn't defined anywhere, either define it, or replace it with one of your already defined variables.
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  3. The Following User Says Thank You to newbie For This Useful Post:

    Delacratic (February 8th, 2011)

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

    Default Re: New to Programming


    Scanner keyboard = new Scanner(System.in);
    monthlyPayment = keyboard.nextDouble();

    System.out.println("Enter the outstanding balance of the account:");

    Scanner keyboard = new Scanner(System.in);
    First error is redeclaration of the variable keyboard.


    Secondly in the code below , you are joining string with variable for that , you need to use + operator.

    System.out.println("Interest paid-"+ paidInterest);

    System.out.println("Interest paid-" paidInterest);
    System.out.println("Amount applied to principal" paidPrincipal);
    System.out.println("After the payment, the account balance is:" accountBalance);

    Third error is

    annualInterestRate = .0625;

    you hadn't declare this variable.
    Thanks and Regards
    Dan Brown

    Common Java Mistakes

  5. The Following User Says Thank You to DanBrown For This Useful Post:

    Delacratic (February 8th, 2011)

  6. #4
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: New to Programming

    Not to be picky, but was there something insufficient with my post that made you feel you needed
    to repeat it? Not having a go, just very curious
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

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

    Smile Re: New to Programming

    Not to be picky, but was there something insufficient with my post that made you feel you needed
    to repeat it? Not having a go, just very curious
    No dear.Problem is both of had posted the reply on the same time.
    Thanks and Regards
    Dan Brown

    Common Java Mistakes

  8. #6
    Junior Member
    Join Date
    Feb 2011
    Posts
    3
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: New to Programming

    Thank you both for replying, it is greatly appreciated. After attempting to fix the errors you pointed out, this is the code I have:

    import java.util.*;
     
    public class proj1
    {
            public static void main(String[] args) 
            {	double monthlyPayment, annualInterest, 
    		           outstandingBalance, accountBalance, paidInterest,
                       paidPrincipal;
     
    		annualInterest = .0625;             		
     
    		System.out.println("Enter this month's payment: ");
     
    		Scanner keyboard = new Scanner(System.in);
    		monthlyPayment  = keyboard.nextDouble();		
     
    		System.out.println("Enter the outstanding balance of the 
    account: ");
     
    		outstandingBalance = keyboard.nextDouble();  
     
    		paidInterest = (annualInterest / 12) *  
    				outstandingBalance;
    		paidPrincipal = monthlyPayment - paidInterest;
    		accountBalance = outstandingBalance - paidPrincipal;
     
    		System.out.println("Here is a breakdown of the 
    payment: ");
    		System.out.println("Interest paid- " + paidInterest);
            System.out.println("Amount applied to principal- " +
    paidPrincipal);
    		System.out.println("After the payment, the account 
    balance is: " + accountBalance);
    	}
    }

    After compiling, these are the errors I received:

    proj1.java:17: unclosed string literal
    System.out.println("Enter the outstanding balance of the
    ^
    proj1.java:17: ';' expected
    System.out.println("Enter the outstanding balance of the
    ^
    proj1.java:18: illegal start of expression
    account: ");
    ^
    proj1.java:18: unclosed string literal
    account: ");
    ^
    proj1.java:27: unclosed string literal
    System.out.println("Here is a breakdown of the
    ^
    proj1.java:27: ';' expected
    System.out.println("Here is a breakdown of the
    ^
    proj1.java:28: illegal start of expression
    payment: ");
    ^
    proj1.java:28: unclosed string literal
    payment: ");
    ^
    proj1.java:32: unclosed string literal
    System.out.println("After the payment, the account
    ^
    proj1.java:32: ';' expected
    System.out.println("After the payment, the account
    ^
    proj1.java:33: unclosed string literal
    balance is: " + accountBalance);
    ^
    11 errors
    Last edited by Delacratic; February 8th, 2011 at 02:14 PM.

  9. #7
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: New to Programming

    It's just a simple problem you have, to fix it just put all your println contents on the same line: as such
            System.out.println("Enter the outstanding balance of the account: ");
    Or if you do want to keep print statements going on multiple lines, be sure to close off the String, use the + operator and open up the new String continuation, as such:

            System.out.println("This is a very very very very"
                    + "long String indeed...");


    Off-topic again:
    No dear.Problem is both of had posted the reply on the same time.
    Reason I said it was because your post date was: "Yesterday 07:20 AM"
    whereas mine was "06-02-2011 10:02 PM". Just saying
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  10. The Following User Says Thank You to newbie For This Useful Post:

    Delacratic (February 8th, 2011)

  11. #8
    Junior Member
    Join Date
    Feb 2011
    Posts
    3
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: New to Programming

    Thank you very much The program works now.

Similar Threads

  1. Programming noob here, need help!
    By artenuga54 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: September 18th, 2010, 04:04 PM
  2. new to java programming GUI help
    By eg6_kaydoo in forum AWT / Java Swing
    Replies: 4
    Last Post: July 14th, 2010, 09:55 PM
  3. New to programming
    By Milanocookies56@gmail.com in forum What's Wrong With My Code?
    Replies: 5
    Last Post: March 31st, 2010, 09:55 AM
  4. Free Programming
    By freeprogramming in forum The Cafe
    Replies: 1
    Last Post: March 14th, 2010, 03:21 PM
  5. Please help me with this Java programming
    By simply_stunning79 in forum Algorithms & Recursion
    Replies: 1
    Last Post: February 22nd, 2010, 07:48 PM

Tags for this Thread