-1

I am in a Java class and I have a sales program as a project. My code will run but will not accept a second user's name as necessary in the code. It then makes a comparison between the two salaries. But my second calculation is not working correctly. Here is what I have:

package sales2;

import java.util.Scanner;

public class Sales2 {

public static void main(String[] args) {
    Scanner input = new Scanner(System.in);                                 // Initialise scanner

    String[] empName;
    empName = new String[2];

    double[] annualSales;
    annualSales = new double[2];

    System.out.printf("Please enter the employees name:");                  // Question and input
    empName[0] = input.nextLine();

    System.out.printf("Please enter your annual sales:");
    annualSales[0] = Double.parseDouble(input.nextLine());

        double salary1 = Utils2.calculateSalary(annualSales[0]);             // Read sales from input & calculate salary


    System.out.println(empName[0] + "'s total yearly salary is: " + Utils2.numFormat(salary1));    // Print information for user

        input.nextLine();

    System.out.printf("Please enter the employees name:");
    empName[1] = input.nextLine();

    System.out.printf("Please enter your annual sales:");
    annualSales[0] = Double.parseDouble(input.nextLine());

        double salary2 = Utils2.calculateSalary(annualSales[1]);                    // Read sales from input & calculate salary


    System.out.println(empName[1] + "'s total yearly salary is: " + Utils2.numFormat(salary2));   // Print information for user

    if (salary1 > salary2){
        System.out.println(empName[0] + " has a higher total annual compensation.");
        System.out.println(empName[1] + " will need to increase their sales to match or exceed " 
        + empName[0] + ", here is how much :" + (salary1 - salary2) );
    }else if (salary1 < salary2){
        System.out.println(empName[1] + " has a higher total annual compensation.");
        System.out.println(empName[0] + " will need to increase their sales to match or exceed " 
        + empName[1] + ", here is how much :" + (salary2 - salary1) );
    }else {
    System.out.println("\nBoth Salespersons have equal total annual compensation.");
    }
}
}


/*
 * 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 sales2;

/**
 *
 * @author etw11
 */
import java.text.DecimalFormat;

public class Utils2 {
     public final static double FIXED_SALARY = 30000;
    /**
     * @param dec
     * @return 
     */
    public static String numFormat(double dec) {
         return new DecimalFormat("##,##0.00").format(dec);
    }

    /**
     * Calculates the salary based on the given sales.
     * @param sales The annual sales
     * @return The calculated salary.
     */
    public static double calculateSalary(double sales) {
        double commissionRate = 0.10d;

        if (sales < 320000) {
            commissionRate = 0.00d;
        } else if (sales >= 320000 && sales < 400000) {
            commissionRate = 0.08d;
        }

        // System.out.println("The current commission is " + (int)
(commissionRate * 100) + "% of total sales.");

        return FIXED_SALARY + (sales * commissionRate);
    }
}
  • 1
    see http://stackoverflow.com/questions/13102045/scanner-is-skipping-nextline-after-using-next-nextint-or-other-nextfoo – Scary Wombat Mar 21 '17 at 06:38

2 Answers2

0

change this:

double annualSales = input.nextDouble(); 

to this:

double annualSales = Double.parseDouble(input.nextLine());
msagala25
  • 1,716
  • 2
  • 16
  • 23
  • That is perfect thank you. How would I make a comparison of salaries next between the elements in the array with the input names – mentorCoder Mar 21 '17 at 06:51
  • try to use a **List** as your data collection, and you can sort it by name using Comparable and Collections.sort();. – msagala25 Mar 21 '17 at 06:56
  • check this, http://beginnersbook.com/2013/12/java-arraylist-of-object-sort-example-comparable-and-comparator/ – msagala25 Mar 21 '17 at 06:59
  • I see how arraylist would be very helpful in this situation but how do you utilize it with input and have it output the correct data and be able to make comparisons? – mentorCoder Mar 21 '17 at 07:14
  • When I say I need to compare the employees, it needs to make a statement about the difference in sales and salary and how much more would need to be made in order to match or exceed the higher employee. I have a hard time with array and arraylist and am unsure how to do this. – mentorCoder Mar 22 '17 at 04:53
  • You are currently storing the annualSales for both employees in the same variable, effectively overwriting the first with the second. Instead, you could use an array `double[] annualSales = new double[2]` and you could store the first value in with `annualSales[0] = Double.parseDouble(input.nextLine());` and on the next pass, the second value with `annualSales[1] = Double.parseDouble(input.nextLine());`. Notice I have explicitly indexed with `1` and `2`, but you would use the for loop index `i`. Then you can compare like this `double difference = annualSales[1] - annualSales[2];` – Matt Mar 22 '17 at 07:29
  • It would be better practise to use a create an `Employee` class to contain your employee data, and store them in a `List`, but perhaps that is too much at this early stage. Use the array for now. – Matt Mar 22 '17 at 07:30
  • I though the statement Sales2[] array = new Sales2[2]; was the array and taking the values for both the String and the double. Do I have it placed or named incorrectly? – mentorCoder Mar 23 '17 at 00:25
0

Found my issue. In this line:

annualSales[0] = Double.parseDouble(input.nextLine());

It should be:

annualSales[1] = Double.parseDouble(input.nextLine());