brian
brian

Reputation: 1

How to multiply in Java

I am making a button that when pressed it multiplies a number by two but i keep getting an error saying invalid assignment operator and the red is underling the * which should mean multiply in java right?

mult.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        ---->   counter  *2;
        display.setText("Your total is " + counter);
    }
});

Upvotes: 0

Views: 12132

Answers (5)

boi yeet
boi yeet

Reputation: 86

mult.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        int counter_output=counter  *2;
        display.setText("Your total is " + counter_output);
    }
    });

This would save the multiplication to an int variable, and then printing the variable with the saved multiplication would help to solve your problem.

Upvotes: 0

Akash Baran Pal
Akash Baran Pal

Reputation: 1

// multiply without *
        int num1 = 3, num2 = 5;
        int mul = 0,j=0;
        while(j<num2)
        {
        for (int i = 0; i < num2; i++) {
            mul = mul + num1;
            j++;
        }
        System.out.println("Multiple "+mul);

Upvotes: -1

Kumar Vivek Mitra
Kumar Vivek Mitra

Reputation: 33534

I think you totally unintentionally missed the Assignment operator and the Lvalue on the Left Side to hold the resultant...

Ok...see this..

int counter = 0;

mult.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {

        counter = counter * 2;     

        display.setText("Your total is " + counter);
    }
});

Upvotes: 0

flagoworld
flagoworld

Reputation: 3194

----> is not an operator.

counter*2 will multiply counter by 2 leaving counter as is.

Option 1:

counter=counter*2;

Option 2:

counter*=2;

Upvotes: 2

cliffycheng
cliffycheng

Reputation: 381

Presuming there's no code before the counter * 2, it should either be counter = counter * 2; or counter *=2; You're not actually setting anything just by saying * 2 :P

Hopefully counter is a global variable so that it's actually saved outside of the method haha.

Upvotes: 1

Related Questions