# Integer cannot be cast into Float?

**URL:** https://discourse.processing.org/t/integer-cannot-be-cast-into-float/3647
**Category:** Coding Questions
**Created:** [September 18, 2018, 3:45pm UTC](https://discourse.processing.org/t/integer-cannot-be-cast-into-float/3647 "2018-09-18T15:45:53Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![aaronshenhao](https://avatars.discourse-cdn.com/v4/letter/a/ea666f/32.png) [@aaronshenhao](https://discourse.processing.org/u/aaronshenhao)
#### Post date: [September 18, 2018, 3:45pm UTC](https://discourse.processing.org/t/integer-cannot-be-cast-into-float/3647/1 "2018-09-18T15:45:53Z")

</div>

Hi everyone, I have a “Node” class which contains an ‘attributes’ variable. The variable contains a variety of data types associated with a String key. I’m using a HashMap to implement it.

However, when I try and retrieve a float attribute and cast it to a float (this seems to work for PVectors, custom Node classes, boolean, but not floats), Java gives an error that “java.lang.Integer cannot be cast to java.lang.Float”.

Attached is a 5 line code written to demonstrate the problem:

```auto
// Create a HashMap which stores attributes which can consist of different data types.
HashMap<String, Object> attributes = new HashMap<String, Object>();

// Example usage (first one is the problem)
attributes.put("dist", 0);
attributes.put("visited", true);

// Print statement is necessary for error to show
float dist = (float)attributes.get("dist");
println(dist); 
// Output - ClassCastException: java.lang.Integer cannot be cast to java.lang.Float

```

Thanks for helping, Aaron.

---

<div class="post-metadata">

### Author: ![jb4x](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jb4x/32/789_2.png) [@jb4x](https://discourse.processing.org/u/jb4x)
#### Post date: [September 18, 2018, 3:48pm UTC](https://discourse.processing.org/t/integer-cannot-be-cast-into-float/3647/2 "2018-09-18T15:48:16Z")

</div>

Hi Aaron,

Try to first store the value in a int variable and then cast that variable.

It worked for me in the past.

---

<div class="post-metadata">

### Author: ![aaronshenhao](https://avatars.discourse-cdn.com/v4/letter/a/ea666f/32.png) [@aaronshenhao](https://discourse.processing.org/u/aaronshenhao)
#### Post date: [September 18, 2018, 3:53pm UTC](https://discourse.processing.org/t/integer-cannot-be-cast-into-float/3647/3 "2018-09-18T15:53:52Z")

</div>

Would the value be rounded up if I stored it as an int variable? I still require some decimals. I tried storing the value as an int variable by casting it into an int first, but now the variable cannot be cast into a float still. I’m only less than a week old to Java, so I might be missing something obvious, however I couldn’t find anything on Integer to Float errors for Java on google.

---

<div class="post-metadata">

### Author: ![jb4x](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jb4x/32/789_2.png) [@jb4x](https://discourse.processing.org/u/jb4x)
#### Post date: [September 18, 2018, 3:59pm UTC](https://discourse.processing.org/t/integer-cannot-be-cast-into-float/3647/4 "2018-09-18T15:59:17Z")

</div>

I think it’s because you return an Object type.

If you want to return float, you need to write:  
HashMap\<String, Float\>

---

<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: [September 18, 2018, 3:59pm UTC](https://discourse.processing.org/t/integer-cannot-be-cast-into-float/3647/5 "2018-09-18T15:59:35Z")

</div>

The `0` in `attributes.put("dist", 0);` becomes an Integer.  
Use `0.0` instead, so it is stored as a Float: `attributes.put("dist", 0.0);`  
Otherwise, use `(Integer)` as cast: `float dist = (Integer) attributes.get("dist");`

---

<div class="post-metadata">

### Author: ![aaronshenhao](https://avatars.discourse-cdn.com/v4/letter/a/ea666f/32.png) [@aaronshenhao](https://discourse.processing.org/u/aaronshenhao)
#### Post date: [September 18, 2018, 4:03pm UTC](https://discourse.processing.org/t/integer-cannot-be-cast-into-float/3647/6 "2018-09-18T16:03:23Z")

</div>

Aha, that worked, thank you to both of you! `atttributes.put("dist", 0f)` worked for me. I’ll probably stick to the first method, because the second method produces an error if I try to initialize a float value.

---

<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: [September 18, 2018, 4:08pm UTC](https://discourse.processing.org/t/integer-cannot-be-cast-into-float/3647/7 "2018-09-18T16:08:30Z")

</div>

> [@aaronshenhao](#):
>
> … `atttributes.put("dist", 0f)` worked for me.

There are many ways to represent the value `0` as a `float` in Processing: 😉  
`0.0`, `0.`, `.0`, `0f`, `0e0`, `(float) 0`, `0*PI`, etc.
