Reputation: 25
I am creating a program based on arrays that helps calculate the stipend for a number of tutors who assist students with their skills. The program takes in the number of tutors, then proceeds with the tutors name, asks how many students that tutor has assisted and then asks what degree level the tutor has (BS, MS, PhD). The tutor will be payed a different amount of money based on the degree level they have. So if the first tutor (Bill) has taught 5 students and has a PhD (which is $20 per student), then he will be payed $100.00 and, so on and so forth for the other tutors (MS = 15.00 per student, BS = 9.50 per student).
My problem is that when I have entered all the data, I just get 0.0 for the pay column and 0.0 for the total stipend. What I am trying to do is based on the degree entered in the degree array, you multiply the taught array (aka the number of students taught) by the amount of money per student, and then store that number in a new array called stipend. I have re-arranged, re-named, and done about everything I can think of to try and get the stipend to show up but I am running out of options and becoming very frustrated. So if anyone could please help me it would be greatly appreciated.
for(int pay=0; pay<numOfTutors; pay++) {
if (degree[pay] == "BS") {
stipend[pay] = taught[pay] * 9.50;
}
else if (degree[pay] == "MS") {
stipend[pay] = taught[pay] * 15.00;
}
else if (degree[pay] == "PhD") {
stipend[pay] = taught[pay] * 20.00;
}
sum+=stipend[pay];
}
Upvotes: 2
Views: 125
Reputation: 3408
The problem is that you are using the ==
operator, instead of the .equals()
method, to check for string equality. You should change the lines of the form
degree[pay] == "some_string"
to
degree[pay].equals("some_string")
Or, if you want case insensitive matching, you could use
degree[pay].equalsIgnoreCase("some_string")
Here's what's going on: when you write degree[pay] = "some_string"
, you are checking for reference equality, which will not work for what you are doing, because a constant string and a generated string will not have equal references. The .equals()
method will check for equal values, which is what you want.
Upvotes: 2
Reputation: 159754
Use String#equals
to compare String
content. The ==
operator is used to compare Object
references. Because the references of the 2 values being compared are not equals, then none of the pay adjustments for the array stipend
take place. Replace
if (degree[pay] == "BS") {
with
if (degree[pay].equals("BS")) {
Similarly for "MS" and "PhD" String checks.
Upvotes: 2