Zwiebel
Zwiebel

Reputation: 1615

If-then-else statement doesn't work

I have a main class, what looks like this:

class Main {
    public static void main (String[] args) {
        Mobil one = new Mobil ("xxxxxx", "yyyyyy", 00000001, true);
        Mobil two = new Mobil ("yyyyyy", "xxxxxx", 10245624, false);

        one.touchcontrol();
        two.touchcontrol();
        }
}

And I have this Mobil class:

class Mobil {
    String type;
    String manufactureat;
    int modellnumber;
    boolean touchtype;

public Mobil (String manufacturer, String inittype, int number, boolean touch) {
        manufacturer = manufactureat;
        inittype = type;
        number = modellnumber;
        touch = touchtype;
}
public void touchcontrol() {
    if (touchtype == false) 
    {
        System.out.println("This model, has not got Touchscreen!");
    }
    else
    {
        System.out.println("This model, has Touchscreen!");
    }
}

But when I run the program and invoke the one.touchcontrol(); and two.touchcontrol(); it shows that no model has got Touchscreen. I don't know what I missed.

Upvotes: 0

Views: 376

Answers (2)

Asterisk
Asterisk

Reputation: 3574

You are incorrectly assigning values to variables in your constructor...

public Mobil (String manufacturer, String inittype, int number, boolean touch) {
        manufacturer = manufactureat; // should be  manufactureat = manufacturer;
        inittype = type;               //same problem
        number = modellnumber;         // same here
        touch = touchtype;               // and here
}

Upvotes: 1

BalusC
BalusC

Reputation: 1108642

You need to swap the variable assignments in the constructor.

manufactureat = manufacturer;
type = inittype;
modellnumber = number;
touchtype = touch;

In variable assignments in Java (and in pretty much all other languages), the left hand will retrieve the value of the right hand.

See also:

Upvotes: 7

Related Questions