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

Thread: Changing Variable Within Method

  1. #1
    Junior Member
    Join Date
    Dec 2017
    Posts
    18
    Thanks
    13
    Thanked 1 Time in 1 Post

    Default Changing Variable Within Method

    Hello Guys, I am currently trying to generate a table output based upon commission rate. With that in mind I am attempting to alter the "rate" variable given a series of "if statements". Here is my code:

    public class table_output{
    	public void sales(double salesAmount) {
                DecimalFormat formatter = new DecimalFormat("#,###.00"); 
     
                double Table_cap = salesAmount * 1.5;
                int fixedsalary = 50000;
     
                for(double counter = salesAmount; counter < Table_cap;counter +=5000) {
     
               if (counter < 96000) {
                   double rate = 0.00; 
               }
     
               else if(counter >= 96000 && counter <= 120000) {
                   double rate = 0.15; 
               }
     
               else {
                   double rate = 0.15 * 1.25; 
               }
     
    double commissionIncome = counter * rate;
     
    //Calculate totalIncome
                    double totalIncome = commissionIncome + fixedsalary;
    System.out.println(formatter.format(counter))+ " \t " + (formatter.format(totalIncome));
    }

    However I receive an error to "create local variable 'rate', when trying to formulate double commisionIncome

    Please help, and thank you

    - Deployment

  2. #2
    Member John Joe's Avatar
    Join Date
    Jun 2017
    Posts
    261
    My Mood
    Amused
    Thanks
    8
    Thanked 18 Times in 18 Posts

    Default Re: Changing Variable Within Method

    However I receive an error to "create local variable 'rate', when trying to formulate double commisionIncome
    Declare your rate as global variable
    Last edited by John Joe; December 13th, 2017 at 07:31 PM.
    Whatever you are, be a good one

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

    Deployment (December 14th, 2017)

  4. #3
    Junior Member
    Join Date
    Dec 2017
    Posts
    18
    Thanks
    13
    Thanked 1 Time in 1 Post

    Default Re: Changing Variable Within Method

    Quote Originally Posted by John Joe View Post
    Declare your rate as global variable
    I tried this, but nothing is outputting in this file. Here is the code now

    /*
     * To change this license header, choose License Headers in Project Properties.
     * To change this template file, choose Tools | Templates
     * and open the template in the editor.
     */
    package salesperson_part1;
     
    import java.text.DecimalFormat;
     
    public class table_output{
        public static double rate;
    	public void sales(double salesAmount) {
     
                DecimalFormat formatter = new DecimalFormat("#,###.00"); 
     
                double Table_cap = salesAmount * 1.5;
                int fixedsalary = 50000;
     
                for(double counter = salesAmount; counter < Table_cap;counter +=5000) {
     
               if (counter < 96000) {
                   table_output.rate = 0.00; 
               }
     
               else if(counter >= 96000 && counter <= 120000) {
                   table_output.rate = 0.15; 
               }
     
               else {
                   table_output.rate = 0.15 * 1.25; 
               }
     
    double commissionIncome = counter * table_output.rate;
     
    //Calculate totalIncome
                    double totalIncome = commissionIncome + fixedsalary;
     
    System.out.println(formatter.format(counter))+ " \t " + (formatter.format(totalIncome));
    }
            }
     
            }


    --- Update ---

    Quote Originally Posted by John Joe View Post
    Declare your rate as global variable
    Here is the output file

    /*
     * This class handles Commission and Total Annual Compensation Output"
     */
     
    package salesperson_part1;
    import java.text.DecimalFormat;  //For Decimal Format
     
    public class output{
    	public void sales(double salesAmount) {
     
                double basecommission = 0.15;
     
                    //Calculate 15% Commmision
    		double commission = (salesAmount * basecommission); 
     
                     //Set Decimal Format
                    DecimalFormat formatter = new DecimalFormat("#,###.00");
     
                    if (salesAmount <= (0.80 * 120000)) {
     
                    //Output Total Commission
    		System.out.print("\nYour Total Commission is $0");
     
                    //Calculate Anual Compensation
    		double fixedSalary = 50000;
     
                    //Output Total Anual Compensation
    		System.out.print("\nYour Total Annual Compensation is your base "
                            + "salary of $" + 
                            (formatter.format(fixedSalary)) + "\n\n");
                     }
     
                    else if (salesAmount < 120000 && salesAmount > (0.80*120000)){
     
     
                     System.out.print("\nYour Total Commission is $" + 
                            (formatter.format(commission)));
     
                    //Calculate Anual Compensation
    		double AnnualCompensation = commission + 50000;
     
                    //Output Total Anual Compensation
    		System.out.print("\nYour Total Annual Compensation is $" + 
                            (formatter.format(AnnualCompensation)) + "\n\n"); 
     
    	     }
     
                    else if (salesAmount >= 120000){
     
                        double extracommission = ((1.25 * basecommission) * salesAmount);
     
                     System.out.print("\nYour Total Commission is $" + 
                            (formatter.format(extracommission)));
     
                    //Calculate Anual Compensation
    		double AnnualCompensation = extracommission + 50000;
     
                    //Output Total Anual Compensation
    		System.out.print("\nYour Total Annual Compensation is $" + 
                            (formatter.format(AnnualCompensation)) + "\n\n"); 
     
    	     }
     
            }
    }


    --- Update ---

    Quote Originally Posted by Deployment View Post
    I tried this, but nothing is outputting in this file. Here is the code now

    /*
     * To change this license header, choose License Headers in Project Properties.
     * To change this template file, choose Tools | Templates
     * and open the template in the editor.
     */
    package salesperson_part1;
     
    import java.text.DecimalFormat;
     
    public class table_output{
        public static double rate;
    	public void sales(double salesAmount) {
     
                DecimalFormat formatter = new DecimalFormat("#,###.00"); 
     
                double Table_cap = salesAmount * 1.5;
                int fixedsalary = 50000;
     
                for(double counter = salesAmount; counter < Table_cap;counter +=5000) {
     
               if (counter < 96000) {
                   table_output.rate = 0.00; 
               }
     
               else if(counter >= 96000 && counter <= 120000) {
                   table_output.rate = 0.15; 
               }
     
               else {
                   table_output.rate = 0.15 * 1.25; 
               }
     
    double commissionIncome = counter * table_output.rate;
     
    //Calculate totalIncome
                    double totalIncome = commissionIncome + fixedsalary;
     
    System.out.println(formatter.format(counter))+ " \t " + (formatter.format(totalIncome));
    }
            }
     
            }


    --- Update ---



    Here is the output file

    /*
     * This class handles Commission and Total Annual Compensation Output"
     */
     
    package salesperson_part1;
    import java.text.DecimalFormat;  //For Decimal Format
     
    public class output{
    	public void sales(double salesAmount) {
     
                double basecommission = 0.15;
     
                    //Calculate 15% Commmision
    		double commission = (salesAmount * basecommission); 
     
                     //Set Decimal Format
                    DecimalFormat formatter = new DecimalFormat("#,###.00");
     
                    if (salesAmount <= (0.80 * 120000)) {
     
                    //Output Total Commission
    		System.out.print("\nYour Total Commission is $0");
     
                    //Calculate Anual Compensation
    		double fixedSalary = 50000;
     
                    //Output Total Anual Compensation
    		System.out.print("\nYour Total Annual Compensation is your base "
                            + "salary of $" + 
                            (formatter.format(fixedSalary)) + "\n\n");
                     }
     
                    else if (salesAmount < 120000 && salesAmount > (0.80*120000)){
     
     
                     System.out.print("\nYour Total Commission is $" + 
                            (formatter.format(commission)));
     
                    //Calculate Anual Compensation
    		double AnnualCompensation = commission + 50000;
     
                    //Output Total Anual Compensation
    		System.out.print("\nYour Total Annual Compensation is $" + 
                            (formatter.format(AnnualCompensation)) + "\n\n"); 
     
    	     }
     
                    else if (salesAmount >= 120000){
     
                        double extracommission = ((1.25 * basecommission) * salesAmount);
     
                     System.out.print("\nYour Total Commission is $" + 
                            (formatter.format(extracommission)));
     
                    //Calculate Anual Compensation
    		double AnnualCompensation = extracommission + 50000;
     
                    //Output Total Anual Compensation
    		System.out.print("\nYour Total Annual Compensation is $" + 
                            (formatter.format(AnnualCompensation)) + "\n\n"); 
     
    	     }
     
            }
    }
    and the initial file

    /*
     * This program collects SalesPerson Annual Sales and performs a 15%
     * commission upon these sales. The 15% commission is accumulated with the fixed
     * salary amount to output a Total Annual Compensation
     *
     */
     
    package salesperson_part1;
     
    // For BufferReader
    import java.io.IOException; 
    import java.io.InputStreamReader;  
    import java.io.BufferedReader; 
    //For Deciman Format
    import java.text.DecimalFormat;
     
     
    public class Salesperson_Part1 {
        public static double salesAmount;  
        public static void main(String[] args) { 
     
             // SalesPerson Fixed Salary
        int fixedSalary = 50000;
     
             //Set Decimal Format
                 DecimalFormat formatter = new DecimalFormat("#,###.00");
     
             //Brief Description for User
                 System.out.println("This program will calculate your Total Annual"
                       + " Compensation.\nTotal Annual Compensation is an "
                       + "accumulation of your fixed salary of " + 
                         (formatter.format(fixedSalary)) + " and benefits based upon"
                                 + " total sales.");
     
             //Declare Amount Sold Variable
     
     
             //Collect Amount Sold Data
                 System.out.print("\nWhat is your total sales amount for the year?"
                         + " (Do not use commas): ");
     
            //Check for Correct Entry Data
             try { 
                BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); 
                String line = in.readLine().trim(); 
                   if (line == null || line.length() == 0) { 
                      System.out.println("Canceled."); 
                      return; 
                } 
     
                //Declare SalesAmount
                salesAmount = Double.parseDouble(line);  
     
                //Pass SalesAmount to Output Class
                output o = new output();
                o.sales(salesAmount); 
     
                table_output t = new table_output();
                o.sales(salesAmount);
     
     
     
                //Continue Check for Correct Entry Data
            }   catch (NumberFormatException exception) { 
                     System.out.println("Error: Not a valid sales amount."); 
            }   catch (IOException exception) { 
                     System.out.println("Error: " + exception.getLocalizedMessage()); 
            } 
     
     
     
        }
     
     
    }

  5. #4
    Member John Joe's Avatar
    Join Date
    Jun 2017
    Posts
    261
    My Mood
    Amused
    Thanks
    8
    Thanked 18 Times in 18 Posts

    Default Re: Changing Variable Within Method

    What you mean by nothing is outputting in this file?
    Whatever you are, be a good one

Similar Threads

  1. help with defining a changing variable within a loop
    By uswhovian in forum What's Wrong With My Code?
    Replies: 7
    Last Post: March 9th, 2013, 10:30 AM
  2. help with defining a changing variable within a loop
    By uswhovian in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 9th, 2013, 09:30 AM
  3. help with defining a changing variable within a loop
    By uswhovian in forum Loops & Control Statements
    Replies: 3
    Last Post: March 9th, 2013, 09:30 AM
  4. calling a changing variable from another class
    By bondage in forum Collections and Generics
    Replies: 11
    Last Post: December 7th, 2011, 09:17 AM
  5. JButton Auto-changing Reference Variable
    By bgroenks96 in forum What's Wrong With My Code?
    Replies: 6
    Last Post: June 19th, 2011, 10:57 PM