-3

I'm passing three values to a function from the main. With these values I make a comparison with an IF statement. The condition has to be that all three parameters have to be verified together, with AND(&&) statement.

This is the main when I pass the values

public static void main(String args[]) {

        start();


        goal();

        load_truck("truck", "cdg", "p1");


    }

This is the function that receive them

public static void load_truck(String truckL, String placeL, String packL) {

        packL = pack;
        truckL = truck;
        placeL = city;
        placeL = airport;
        placeL = loc;


        if(truckL == "truck" && placeL == "cdg" && pack == "p1") {

            at_obj_place(pack = "p1", placeL = "");

            in_pk_vehicle(pack = "p1", truck = "truck");

            System.out.println("\n"+ "The pack "+ pack + " is on the "+ truck );
        }

        if(truckL == "truck" && placeL == "cdg" && pack == "p2") {

            at_obj_place(pack = "p2", placeL = "");

            in_pk_vehicle(pack = "p2", truck = "truck");

            System.out.println("The pack "+ pack + "is on the "+ truck );
        }
        }

The problem is that the comparison with the IF statement is not working. If I insert just one value to compare is working, but with 2 o 3 the IF doesn't work. What is the problem?

EDIT: the problem was how I was filling the variables in the method. I removed and it's working also with ==, not only with equals(). Thanks to everyone

Mike994
  • 55
  • 8
  • 5
    Possible duplicate of [How do I compare strings in Java?](https://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – Turamarth Oct 15 '19 at 11:04
  • 2
    It's not the `if` statement *not working*... It's your comparison of `String`s. Use `equals()` instead of `==`. – deHaar Oct 15 '19 at 11:04
  • As other users have already shown, you shouldn't compare `String` with `equals()`, not with `==`. I would like to add: check the Java coding conventions (camelCase instead of names_with_underscores). Good luck with your homework! – almac777 Oct 15 '19 at 11:09
  • Is not working also with equals() – Mike994 Oct 15 '19 at 11:10

3 Answers3

0

This:

at_obj_place(pack = "p1", placeL = "");

isn't java. Java does not have named parameters.

NB: The commentary about 'use a.equals(b) and not a == b to compare strings' is ALSO valid; this code has more than one issue.

A friendly suggestion: When you get compiler errors, or exceptions, always paste them along with your question. It'll help those who try to answer your question :)

rzwitserloot
  • 85,357
  • 5
  • 51
  • 72
  • Yes it is Java: _"At run time, the result of the assignment expression is the value of the variable after the assignment has occurred."_ See https://docs.oracle.com/javase/specs/jls/se11/html/jls-15.html#jls-15.26 – Fildor Oct 15 '19 at 11:33
  • ^-- In other words: Those are _not_ named parameters. They are assignments. `pack="p1"` evaluates to "p1" which is then passed as argument. Same for `placeL=""` – Fildor Oct 15 '19 at 11:54
0

(This should be a comment, but it's too long and needs formatting.)

What is this?

packL = pack;
truckL = truck;
placeL = city;
placeL = airport;
placeL = loc;

You are overwriting the parameters with some other variables. What are the variables pack, truck, etc.?

Also these functions calls are strange:

at_obj_place(pack = "p1", placeL = "");

It's valid Java, but highly unusual. Most likely it's not doing what you think it does.

RoToRa
  • 37,635
  • 12
  • 69
  • 105
0

.equals() method should work for you. However, the code does not looks like a valid java code.