I am working on a project where I return back the values of a sequence of Java methods so that I can spew out the results. But it is being printed out, out of sync. It should come out as the following:
return
"Name: " + getFirstName() + " " + getLastName() + "\n" +
"Id: " + getEmployeeId() + "\n" +
"Hourly Rate: $" + getHourlyRate() + "\n" +
timeCard.toString() +
"Weekly Pay: $" + getWeeklyPay();
But the calling of the timeCard.toString() keeps rising to the top when it should be number 4 in the output. Here is the timeCard.toString() method below:
public String toString()
{
System.out.println("Weekly Hours: " + getWeeklyHours());
for(int i = 0; i < NUMDAYS; i++ )
{
System.out.println(" Day " + (i + 1) + ": " + getHoursByDay(i));
}
return "";//I had to place this hack here so the toString() format is valid.
}
Is there an adhered to order of operations as far as the output using the return keyword is concerned? Or is my code causing it to come in out of order? Please offer your guidance on why this code is coming in out of order? How can I fix it?
Here is the output I am getting:
Employee Id:
1001
Employee Hourly Rate:
56
Enter Hours for day 1:
8
Enter Hours for day 2:
8
Enter Hours for day 3:
8
Enter Hours for day 4:
8
Enter Hours for day 5:
8
Employee:
Weekly Hours: 40
Day 1: 8
Day 2: 8
Day 3: 8
Day 4: 8
Day 5: 8
Name: Stanford Marceles
Id: 1001
Hourly Rate: $56.0
Weekly Pay: $2240
BUILD SUCCESSFUL (total time: 29 seconds)
Cheers,
You can use StringBuilder to create a String, as in the following: