# "Case expressions must be constant expressions" with final int

**URL:** <https://discourse.processing.org/t/case-expressions-must-be-constant-expressions-with-final-int/16109>\
**Category:** Beginners\
**Created:** [December 4, 2019, 11:59am UTC](https://discourse.processing.org/t/case-expressions-must-be-constant-expressions-with-final-int/16109 "2019-12-04T11:59:05Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![TurboSlayer](https://avatars.discourse-cdn.com/v4/letter/t/4491bb/32.png) [@TurboSlayer](https://discourse.processing.org/u/TurboSlayer)\
**Post date:** [December 4, 2019, 11:59am UTC](https://discourse.processing.org/t/case-expressions-must-be-constant-expressions-with-final-int/16109/1 "2019-12-04T11:59:05Z")

</div>

I get an error saying “Case expressions must be constant expressions”. I couldn’t fix it no matter what. Is the only solution to change xSpeed as well as origxSpeed or is there a way to set origxSpeed to xSpeed without getting the error?  
I won’t send the full code because its like 300+ lines long. Here is what I want to do:

```auto
float xSpeed=10
final int origxSpeed=int(xSpeed);
//or do I have to do final int origxSpeed=10?

  if ((key == '1') && game==true) { 
    switch(int(abs(xSpeed))) {
    case origxSpeed:
      xSpeed = xSpeed * 2/3; //for some reason " *= " has trouble with fractions
      ySpeed = ySpeed * 2/3; //for some reason " *= " has trouble with fractions
      break;
    case 2:
      xSpeed *= 1; // *= works here
      ySpeed *= 1;
      break;
    }
  }

```

Also, one more question. Why does " \*= " have trouble with fractions such as 2/3 and how can I fix it? When I tried to use " \*= " with a fraction the value didn’t change at all. It works perfectly fine with regular numbers and decimals. I need to get this done by next week so I need answers ASAP. Thanks!

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [December 4, 2019, 12:05pm UTC](https://discourse.processing.org/t/case-expressions-must-be-constant-expressions-with-final-int/16109/2 "2019-12-04T12:05:44Z")

</div>

> [@TurboSlayer](#):
>
> I need to get this done by next week so I need answers ASAP.

thanks for this information, Turbo

* * *

```auto
println( (2/3) );
println( (2/(float)3) );

```

this is called integer division problem

* * *

better here not use switch / case / break  
it anyhow not makes the code shorter or more readable…  
use a IF and print the THING , what makes the condition for the IF, first.

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [December 4, 2019, 12:07pm UTC](https://discourse.processing.org/t/case-expressions-must-be-constant-expressions-with-final-int/16109/3 "2019-12-04T12:07:08Z")

</div>

> [@TurboSlayer](#):
>
> Why does " \*= " have trouble with fractions such as 2/3 and how can I fix it?

`xSpeed = xSpeed * 2/3;` → `xSpeed *= 2 / 3.0;`  
`ySpeed = ySpeed * 2/3;` → `ySpeed *= 2.0 / 3;`

---

<div class="post-metadata">

**Author:** ![TurboSlayer](https://avatars.discourse-cdn.com/v4/letter/t/4491bb/32.png) [@TurboSlayer](https://discourse.processing.org/u/TurboSlayer)\
**Post date:** [December 4, 2019, 12:10pm UTC](https://discourse.processing.org/t/case-expressions-must-be-constant-expressions-with-final-int/16109/4 "2019-12-04T12:10:53Z")

</div>

> [@GoToLoop](#):
>
> `xSpeed = xSpeed * 2/3;` → `xSpeed *= 2 / 3.0;`  
> `ySpeed = ySpeed * 2/3;` → `ySpeed *= 2.0 / 3;`

Thank you, it works. I didn’t know one of them had to be a float for it to work.

---

<div class="post-metadata">

**Author:** ![TurboSlayer](https://avatars.discourse-cdn.com/v4/letter/t/4491bb/32.png) [@TurboSlayer](https://discourse.processing.org/u/TurboSlayer)\
**Post date:** [December 4, 2019, 12:12pm UTC](https://discourse.processing.org/t/case-expressions-must-be-constant-expressions-with-final-int/16109/5 "2019-12-04T12:12:03Z")

</div>

> [@kll](#):
>
> better here not use switch / case / break  
> it anyhow not makes the code shorter or more readable…

I actually have three cases. All I want to know is whether I can set origxSpeed to int(xSpeed) without that error.

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [December 4, 2019, 12:14pm UTC](https://discourse.processing.org/t/case-expressions-must-be-constant-expressions-with-final-int/16109/6 "2019-12-04T12:14:47Z")

</div>

> [@TurboSlayer](#):
>
> “Case expressions must be constant expressions”.

In Java a constant expression is something that can be pre-calculated at compile-time. 🕰

However, the value of _origxSpeed_ can only be determined at run-time. 🏃‍♂️

If you depend on _origxSpeed_ as 1 of the conditions, you should just use a vanilla `if/else` block. 🧱

---

<div class="post-metadata">

**Author:** ![TurboSlayer](https://avatars.discourse-cdn.com/v4/letter/t/4491bb/32.png) [@TurboSlayer](https://discourse.processing.org/u/TurboSlayer)\
**Post date:** [December 4, 2019, 12:17pm UTC](https://discourse.processing.org/t/case-expressions-must-be-constant-expressions-with-final-int/16109/7 "2019-12-04T12:17:30Z")

</div>

> [@GoToLoop](#):
>
> If you depend on _origxSpeed_ as 1 of the conditions, you should just use a vanilla `if/else` blocks

Otherwise if I want to use switch then I have to change both variables right?

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [December 4, 2019, 12:18pm UTC](https://discourse.processing.org/t/case-expressions-must-be-constant-expressions-with-final-int/16109/8 "2019-12-04T12:18:03Z")

</div>

> [@TurboSlayer](#):
>
> I didn’t know one of them had to be a float for it to work.

Of course, both can be a floating value too. 😉

The rule is: At least 1 of the operands of the operator `/` needs to be a `float` or a `double`; otherwise the result is an `int`, w/ its fractional part removed. 🧐

---

<div class="post-metadata">

**Author:** ![TurboSlayer](https://avatars.discourse-cdn.com/v4/letter/t/4491bb/32.png) [@TurboSlayer](https://discourse.processing.org/u/TurboSlayer)\
**Post date:** [December 4, 2019, 12:30pm UTC](https://discourse.processing.org/t/case-expressions-must-be-constant-expressions-with-final-int/16109/9 "2019-12-04T12:30:47Z")

</div>

Out of interest, is [forum.processing.org](http://forum.processing.org) connected with this website? Many of your posts were on that website when I was looking for answers earlier. If they aren’t, you must be having a hard time answering all the questions that come in here and there. Same goes for some other notable people like Chrisir, Quark etc.

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [December 4, 2019, 12:38pm UTC](https://discourse.processing.org/t/case-expressions-must-be-constant-expressions-with-final-int/16109/10 "2019-12-04T12:38:36Z")

</div>

Processing got a couple of retired forums.  
This 1 here in Discourse is the current active 1.  
And indeed, I link to my old posts from time to time.

---

<div class="post-metadata">

**Author:** ![TurboSlayer](https://avatars.discourse-cdn.com/v4/letter/t/4491bb/32.png) [@TurboSlayer](https://discourse.processing.org/u/TurboSlayer)\
**Post date:** [December 4, 2019, 12:40pm UTC](https://discourse.processing.org/t/case-expressions-must-be-constant-expressions-with-final-int/16109/11 "2019-12-04T12:40:40Z")

</div>

I see. Must be a having a hard time anyway haha. I won’t bother you any longer, thanks for your help!

---

<div class="post-metadata">

**Author:** ![TurboSlayer](https://avatars.discourse-cdn.com/v4/letter/t/4491bb/32.png) [@TurboSlayer](https://discourse.processing.org/u/TurboSlayer)\
**Post date:** [December 6, 2019, 3:34pm UTC](https://discourse.processing.org/t/case-expressions-must-be-constant-expressions-with-final-int/16109/12 "2019-12-06T15:34:39Z")

</div>

Guys stupid question here. I need help with shortening this. bally -= (ballx-width)-imgSize; doesn’t work.

```auto
      bally = bally-(ballx-width)-imgSize;

```

---

<div class="post-metadata">

**Author:** ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)\
**Post date:** [December 6, 2019, 3:47pm UTC](https://discourse.processing.org/t/case-expressions-must-be-constant-expressions-with-final-int/16109/13 "2019-12-06T15:47:12Z")

</div>

> [@TurboSlayer](#):
>
> bally = bally-(ballx-width)-imgSize;

Here you are calculating `ballx - width` subtracting the the result and `imgSize` from `bally` THEN assigning the result to `bally`

> [@TurboSlayer](#):
>
> `bally -= (ballx-width)-imgSize`

Here you are calculating `ballx - width` then subtracting `imgsize` THEN subtracting the result from and assigning\* (-=) to `bally` . You have a double negative

**They are different operations**

The correct statement is  
`bally -= ballx - width + imgSize`
