简体   繁体   中英

Can you tell me why I'm getting a "string cannot be converted to int" error

I have an assignment which asks for everything I have in the code below. That all works fine - I just need to calculate any monthly hours over 160 hours to be paid at 1.5 times the normal hourly rate. My math seems sound and calculates fine:

((hours - 160) * overtime) + (160 * hourlyRate)

But I dont know if I'm putting this if statement in the right method or if it even should be an if statement. My increase/decreasePay methods are working prior to this and they need to stay. I removed some things so it's easier to read.

HourlyWorker Class:

public class HourlyWorker extends Employee
{
private int hours;
private double hourlyRate;
private double monthlyPay;
private double overtime = (1.5 * hourlyRate);

public HourlyWorker(String last, String first, String ID, double rate)
{
   super(last, first, ID);
   hourlyRate = rate;
}

public void setHours(int hours)
{
   this.hours = hours;
}

public int getHours()
{
   return hours;
}

public void setHourlyRate(double rate)
{
   this.hourlyRate = rate;
}

public double getHourlyRate()
{
   return hourlyRate;
}


public double getMonthlyPay()
{
   if (hours > 160)
   {
      monthlyPay = ((hours - 160) * overtime) + (160 * hourlyRate);
   }
   else 
   {
      monthlyPay = hourlyRate * hours;
   }
   return monthlyPay;
}

public void increasePay(double percentage)
{
   hourlyRate *= 1 + percentage / 100;
}

public void decreasePay(double percentage)
{
   hourlyRate *= 1 - percentage / 100;
}

}

What I'm testing with:

public class TestEmployee2
{
   public static void main(String[] args)
   {
   Employee [] staff = new Employee[3];
      HourlyWorker hw1 = new HourlyWorker("Bee", "Busy", "BB1265", 10);
       
      hw1.setHours(200);    
      staff[0] = hw1;

   System.out.println(staff[0].getMonthlyPay());
   staff[0].increasePay(10);
   System.out.println(staff[0].getMonthlyPay());
}
}
Output is:
1600 (initial monthly rate, with 40 overtime hours and 160 regular hours)
1760 (10% increase to the monthlyPay)

Should be:
2006
22

06.6

As @NomadMaker mentioned, the problem is in your addArtist method. Your current method:

   public void addArtist(String artistName, String genre)
   {
      this.artists.add(artist, genre); 
   }

Remember that this.artists is a list which can store Objects of type Artist . Therefore you should create a new artist with the new parameters. Something like:

public void addArtist(String artist, String genre)
   {
      this.artists.add(new Artist(artist, genre)); 
   }

As you might be guessing, you do not have a constructor of Artist with two parameters (should accept name, and genre). Therefore, you should add this constructor to your code:

public Artist(String name, String genre) {
 this.name = name;
 this.genre = genre;
}

Error explanation:

artists is a list, what you are doing when calling this.artist.add(artist, genre) is calling a method which belongs to the list collection that has this signature: add(int index, Artist artist) the index will be the index to place the artist (if the order matters for you).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM