Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I am having a small problem with my Java program. It is probably an easy fix and

ID: 3533209 • Letter: I

Question

I am having a small problem with my Java program. It is probably an easy fix and something simple I am overlooking. I just can not figure it out. I have included both my Java files below. Here is the code that needs to be fixed.


//Retrieve name of 2nd salesperson

System.out.println("What is the name of the 2nd salesperson?");

name = keyboard.nextLine();

salesClerk[1].setName(name);


When the user is asked to enter the 2nd sales persons name, it does display "What is the name of the 2nd salesperson" but it does not allow the user to enter in the name, it skips to the next line "Enter the 2nd salesperson's total yearly sales:"


It does work as it should when the user is asked to enter in the1st salespersons name.


First File-Includes Main


package week5assignment;

import java.util.Scanner;


public class Week5Assignment {

public static void main(String[] args)

{ //Created new object Commission Calculator

SalesPerson salesClerk[] = new SalesPerson[2];

//Created 2 new object for Salesclerks

salesClerk[0] = new SalesPerson();

salesClerk[1] = new SalesPerson();

  

//New scanner input

Scanner keyboard = new Scanner(System.in);


//Retrieve name of 1st salesperson

System.out.println("What is the name of the 1st salesperson?");

String name = keyboard.nextLine();

salesClerk[0].setName(name);

  


//Retrieve total yearly sales for 1st Salesperson

System.out.println("Enter the 1st salesperson's total yearly sales: ");

double val = keyboard.nextDouble();

salesClerk[0].setSales(val);


//Retrieve name of 2nd salesperson

System.out.println("What is the name of the 2nd salesperson?");

name = keyboard.nextLine();

salesClerk[1].setName(name);

  

//Retrieve total yearly sales for 2nd Salesperson

System.out.println("Enter the 2nd salesperson's total yearly sales: ");

val = keyboard.nextDouble();

salesClerk[1].setSales(val);


double totalSum1, totalSum2;

totalSum1 = salesClerk[0].getSales();

System.out.println(salesClerk[0].name +" will make a yearly compensation of: $" + totalSum1);

totalSum2 = salesClerk[1].getSales();

System.out.println(salesClerk[1].name +" will make a yearly compensation of: $" + totalSum2);

if (totalSum1 > totalSum2) {

System.out.print(salesClerk[1].name + " will need to increase his sales by " + " $" + (totalSum1 - totalSum2) +" in order to match or exceed " + salesClerk[0].name +"'s.");

} else if (totalSum2 > totalSum1) {

System.out.print(salesClerk[0].name + " will need to increase his sales by " + " $" + (totalSum2 - totalSum1) +" in order to match or exceed " + salesClerk[1].name +"'s.");

} else {

System.out.println(salesClerk[0].name + " and " + salesClerk[1].name + " will both make a yearly compensation of: $" + totalSum1);

}

}

}


Second File-Includes Class


package week5assignment;


public class SalesPerson

{

//private data

  

public String name;

private double Salary;

private double Sales;

private double SalesTarget;

private double accelerationFactor=1.5;//in % form

  

SalesPerson()

{

this.name = "";

Salary = 0;

Sales = 0;

SalesTarget = 0;

}

//mutators

public void setName(String name) {

this.name = name;

}

  

public void setSalary(double salary) //sets salary

{

Salary = salary;

}

  

public void setSales(double sales) //sets sales

{

Sales = sales;

}

  

  

public void setSalesTarget(double salesTarget) //sets sales target

{

SalesTarget = salesTarget;

}

  

//accessors

public String getName() {

return name;

}

  

public double getSalary()

{

return Salary;

}

  

public double getSales()

{

return Sales;

}

  

public double getCommissionRate() //formula for calculating total compensation either at 20% or 0

{

if( Sales<(SalesTarget*.80)) {

return 0;

} else {

return 20+(accelerationFactor);

}   

}

  

public double getSalesTarget()

{

return SalesTarget;

}

  

public double getCommission() //caculates commission

{

return getCommissionRate()*Sales / 100;

}

  

public double getCompensation() //caculates total compensation

{

return getCommission()+Salary; //caculates total compensation

}   

}



Explanation / Answer

//thanks for linking


quick note : Scanner breaks when you use nextLine() and then other types (nextInt() etc) . The way to work around it is to create a separate Scanner object-- once for everything else and another specifically for nextLine()