I am trying to copy all the elements of the allSalaries array, EXCEPT the first
ID: 3627598 • Letter: I
Question
I am trying to copy all the elements of the allSalaries array, EXCEPT the first two elements, to the workerSalary array. I added a print function to print the new array to ensure my array copied correctly. However, when I print I see the first two elements of the array are 0.0. I need the new array to start with 25000.0 and so on. So my current output is:0.0
0.0
25000.0
18000.0
30000.0
9000.0
12000.0
It needs to be:
25000.0
18000.0
30000.0
9000.0
12000.0
There is probably an issue with my loop, but I am done scratching my head. Thanks!
Here is my code:
public class SalaryCopy
{
public static void main(String[] args)
{
double [] allSalaries = {100000.0, 110000.0, 25000.0, 18000.0,
30000.0, 9000.0, 12000.0};
double [] workerSalaries = new double[allSalaries.length];
for (int i=2; i < allSalaries.length; i++)
{
workerSalaries[i] = allSalaries[i];
}
for (int i=0; i < workerSalaries.length; i++)
{
System.out.println(workerSalaries[i]);
}
}//end printSalary
}//end SalaryCopy class
Explanation / Answer
please rate-thanks
public class SalaryCopy
{
public static void main(String[] args)
{
double [] allSalaries = {100000.0, 110000.0, 25000.0, 18000.0,
30000.0, 9000.0, 12000.0};
double [] workerSalaries = new double[allSalaries.length-2]; /*your workers salaries is actually 2 less then your allsalaries/*
for (int i=0; i < allSalaries.length-2; i++) //don't copy the last 2
{
workerSalaries[i] = allSalaries[i+2 ]; //move everything up 2
}
for (int i=0; i < workerSalaries.length; i++)
{
System.out.println(workerSalaries[i]);
}
}//end printSalary
}
//end SalaryCopy class
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.