Results 1 to 9 of 9
  1. #1
    TJRedeemer is offline Member
    Join Date
    Sep 2013
    Posts
    9
    Rep Power
    0

    Default Help with strings(?)

    Alright, I'm totally confused on how I do this. I need to add a loop to my program. I think I may have the looping down, but I'm confused on the string. Here is the assignment in question:

    CP 1 - Assignment # 2b

    The problem is, the program should ask if the user wants to start the program again, based on a y/n input. But I cant seem to get the program to acknowledge y and n as variables. What do I do?

    Java Code:
     /**
     *Author: Taylor Deemer
     *Program Name: LoanPaymentFinder
     *Date: 9/27/13
     *Class: Computer Programming I
     */
    package loanpaymentfinder;
    
    import java.text.NumberFormat;
    import java.util.Scanner;
    
    
    public class LoanPaymentFinder {
    
    
    
        public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
             
            NumberFormat num = NumberFormat.getCurrencyInstance(); 
             
            double numInputLoan;
            double numInputRate;
            double numInputYears;
            double numMonths;
            double numRateSquared;
            double numOutput;
            double numRatePlusOne;
            boolean restart;
            
            restart = true;
            
            while (restart = true)
            {
                    
            //Input
            System.out.print ("Enter loan amount: ");
            numInputLoan = Double.parseDouble (in.nextLine());
             
            System.out.print ("Enter rate: ");
            numInputRate = Double.parseDouble (in.nextLine());
             
            System.out.print ("Enter number of years: ");
            numInputYears = Double.parseDouble (in.nextLine());
            
            //Checks for negitive inputs
            
            if (numInputLoan < 0)
            {
                System.out.print ("You must enter positive numeric data!");
                System.out.println ("Would you like to calculate again (y/n):");
                
            }
            else if (numInputRate < 0)
            {
                System.out.print ("You must enter positive numeric data!");
            }
            else if (numInputYears < 0)
            {
                System.out.print ("You must enter positive numeric data!");
            }
            else
            {
            //Math goes here
             
            numMonths = 12 * numInputYears;
            
            numInputRate = numInputRate / 1200;
             
            numRatePlusOne = numInputRate + 1;
                    
            numRateSquared = Math.pow (numRatePlusOne, numMonths);
             
            numOutput = numRateSquared/(numRateSquared - 1);
             
            numOutput = numInputRate * numInputLoan * numOutput;
             
            //Output
            System.out.println ("The mounthly payment is:");
            System.out.println (num.format(numOutput));
            }
            }
        }
    }

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    19,433
    Rep Power
    29

    Default Re: Help with strings(?)

    cant seem to get the program to acknowledge y and n as variables
    If you are getting error messages, copy the full text and paste it here.
    I don't see where there are any variables named: y or n in the posted code.

    One problem I see in the code is the use of an assignment statement inside a while:
    while(restart = true)
    That will assign true to restart and return true to the while()

    The while() statement takes a boolean value; a boolean variable will do that:
    while(restart)
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    rang3tastic is offline Member
    Join Date
    Oct 2013
    Posts
    13
    Rep Power
    0

    Default Re: Help with strings(?)

    This block of code can be factored into a smaller block of code
    Java Code:
    if (numInputLoan < 0)
            {
                System.out.print ("You must enter positive numeric data!");
                System.out.println ("Would you like to calculate again (y/n):");
                 
            }
            else if (numInputRate < 0)
            {
                System.out.print ("You must enter positive numeric data!");
            }
            else if (numInputYears < 0)
            {
                System.out.print ("You must enter positive numeric data!");
            }
    could be written as

    Java Code:
    if (numInputLoan < 0 || numInputRate < 0 || numInputYears < 0)
            {
                System.out.print ("You must enter positive numeric data!");
                System.out.println ("Would you like to calculate again (y/n):");
                 
            }
    easier isnt it =p ?

  4. #4
    TJRedeemer is offline Member
    Join Date
    Sep 2013
    Posts
    9
    Rep Power
    0

    Default Re: Help with strings(?)

    Quote Originally Posted by rang3tastic View Post
    This block of code can be factored into a smaller block of code
    Java Code:
    if (numInputLoan < 0)
            {
                System.out.print ("You must enter positive numeric data!");
                System.out.println ("Would you like to calculate again (y/n):");
                 
            }
            else if (numInputRate < 0)
            {
                System.out.print ("You must enter positive numeric data!");
            }
            else if (numInputYears < 0)
            {
                System.out.print ("You must enter positive numeric data!");
            }
    could be written as

    Java Code:
    if (numInputLoan < 0 || numInputRate < 0 || numInputYears < 0)
            {
                System.out.print ("You must enter positive numeric data!");
                System.out.println ("Would you like to calculate again (y/n):");
                 
            }
    easier isnt it =p ?
    I don't tell you how to program your stuff. Smartass.

  5. #5
    rang3tastic is offline Member
    Join Date
    Oct 2013
    Posts
    13
    Rep Power
    0

    Default Re: Help with strings(?)

    Quote Originally Posted by TJRedeemer View Post
    I don't tell you how to program your stuff. Smartass.
    Sounds like you failed the assignment.

  6. #6
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    13,075
    Rep Power
    23

    Default Re: Help with strings(?)

    Quote Originally Posted by TJRedeemer View Post
    I don't tell you how to program your stuff. Smartass.
    You'll go far.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  7. #7
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    9

    Default Re: Help with strings(?)

    Quote Originally Posted by Tolls View Post
    You'll go far.
    Hey, even at McDonalds you can get promoted.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  8. #8
    TJRedeemer is offline Member
    Join Date
    Sep 2013
    Posts
    9
    Rep Power
    0

    Default Re: Help with strings(?)

    Quote Originally Posted by rang3tastic View Post
    Sounds like you failed the assignment.
    For your information, I figured out the problem. BY MYSELF. And yeah, I'm never coming back here.

  9. #9
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,373
    Blog Entries
    7
    Rep Power
    25

    Default Re: Help with strings(?)

    Quote Originally Posted by TJRedeemer View Post
    For your information, I figured out the problem. BY MYSELF. And yeah, I'm never coming back here.
    Good day.

    Jos
    The only person who got everything done by Friday was Robinson Crusoe.

Similar Threads

  1. Help with Strings
    By EatMyChidori in forum New To Java
    Replies: 4
    Last Post: 11-07-2012, 10:42 AM
  2. Need help with strings please!!!!
    By ndsmith20 in forum New To Java
    Replies: 4
    Last Post: 10-24-2012, 11:24 PM
  3. Strings
    By leepikamukharji in forum New To Java
    Replies: 12
    Last Post: 04-18-2011, 01:43 PM
  4. Strings
    By zoala001 in forum Java Applets
    Replies: 9
    Last Post: 01-02-2011, 11:26 AM
  5. It is possible in Strings..?
    By mlibot in forum New To Java
    Replies: 1
    Last Post: 03-12-2010, 06:30 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •