-2

I am trying to work out how to add a value to a variable based on if a String is present in an array. Hopefully my code will explain it better:

//calculate bonus
String[] department = {"Dublin","London","London","Dublin","Paris","Paris"};
int [] brokerTotal =(79,35,55,101,63,108};

int[] bonus;
bonus=new int[6];

for(int i = 0;i < department.length; i++) {
    if (department ="Dublin") bonusRate=12;
    else if (department="London") bonusRate=15;
    else bonusRate=10;
    bonus[i] = ((brokerTotal[i])/100)*bonusRate;
}

The if statements are catching me here, am I close or totally off the mark here?

Konstantin Yovkov
  • 62,134
  • 8
  • 100
  • 147
bIG_aL
  • 113
  • 1
  • 4
  • 12
  • Are you comparing (`==`) or assigning (`=`)? Also, http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java – Sotirios Delimanolis Nov 27 '13 at 15:13
  • im trying to say if eg the department is Dublin then the bonus rate is 12 – bIG_aL Nov 27 '13 at 15:15
  • And do you do that with **assignment** operator or **equality** operator? – Sotirios Delimanolis Nov 27 '13 at 15:19
  • 1
    You should use some IDE if you're learning Java, to correct you for elementary mistakes like this. Google "Eclipse download" or "IntelliJ IDEA community download". Both are free to use. – Dropout Nov 27 '13 at 15:20
  • The other day I've noticed a comment unders similar question that there is no day on SO without someone having problems with comparing strings in Java - looks like it is very true. – Artur Nov 27 '13 at 15:25
  • Ok so it should be the equality operator,thanks. – bIG_aL Nov 27 '13 at 15:27
  • You should read a java tutorial or something instead of asking for help here, then ask here anything that you have trouble with. Comparing strings with `==` is an error in most cases (you should use `.equals`) and comparing anything with `=` is always an error. – aalku Nov 27 '13 at 15:39

3 Answers3

5
if (department="London")

should be

if ("London".equals(department))

Same goes for the if(department ="Dublin") statement

More info:

Community
  • 1
  • 1
Konstantin Yovkov
  • 62,134
  • 8
  • 100
  • 147
  • You would want to use this rather than the '==' operator other answers are suggesting. '==' checks the [equals()](http://www.javapractices.com/topic/TopicAction.do?Id=17) method, which for Strings, checks both Strings location in memory to see if they are the exact same String. – DoubleDouble Nov 27 '13 at 15:31
  • I am aware of what `==` operator does. I believe the OP wants to compare content rather than references, so my answer stands. :) – Konstantin Yovkov Nov 27 '13 at 15:33
  • Sorry, I'm not meaning to correct anything in your answer but actually agree with you and am adding the reason why he would want to use equals without having to click the link. – DoubleDouble Nov 27 '13 at 15:36
  • 1
    OK now I know what to do,sometimes its the simple logic that catches me as I have only started programming this September.Now,as a bonus, I also know the difference between "==" and ".equals()" – bIG_aL Nov 27 '13 at 15:56
0

You're using assignment operator = instead of equality ==

Konstantin
  • 3,254
  • 15
  • 20
  • Right, this is a common mistake. But `==` won't work either, so this answer is somewhat misleading. – ajb Nov 27 '13 at 15:31
  • It will work as it's not comparing to `new String("Dublin")`, but interned one. Having said that, equals is a better option. – Konstantin Nov 27 '13 at 15:40
0
public static void main(String[] args) {

    String[] cars = { " Lamborghini Diablo", "Ford Raptor", "Ferrari Testarossa", "Porsche 911 Carrera",
            "Jensen Interceptor", "Lamborghini Huracán", "Ferrari 812 Superfast", "Jeep Gladiator" };

    for (int i = 0; i < cars.length; i++) {

        if ("Jensen Interceptor".equals(cars[i])) {

            System.out.println("Dump Car");
        }

        System.out.println("Master Piece");

    }

}
  • Shouldn't you add a little comment to explain your answer? – Laurent Nov 17 '21 at 07:05
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Nov 17 '21 at 07:44