How can I get Instance of Integer?

I am trying this code but it didn't store anything.

public void store(Object obj) {
     if (obj instanceof Integer) {
 Toast.makeText(context, "Indeed. Working",Toast.LENGTH_SHORT).show();
}
}

but it won't show any notifier.
I am getting object from YailList.
It looks like that Integer is wrong class.

What are you trying to do?

I want to know that user had put a Int block in make a List block

Do you want to know the input type?

Yeah...... Did you know how to know ??

Yeah I had tried it 2 days ago. Here is an example:

System.out.println((Object) "Hello".getClass() == data.getClass()); // PRINTS TRUE

I want to know that users has put int in make a list block. this one

blocks - 2021-02-11T163642.881

I want to know on 1 index which block has putted.

blocks - 2021-02-11T163812.342

I successfully know the String is putted but not int.

1 Like

Just wait a minute.

Just check if it's number, it's easy, I thought you want to know the class type

SharedScreenshot

You cant. As I know till now.

Int is also a String . I Thing. So, maybe this method won't work

:laughing: :expressionless:

No it's not, integer is integer.

The title is wrong. Instance means new object.

Okay, You are right but why this code won't work.

public void store(Object obj) {
     if (obj instanceof Integer) {
 Toast.makeText(context, "Indeed. Working",Toast.LENGTH_SHORT).show();
}
}

also @KUMARASWAMY_B.G your trick won't work for me.

public void store(Object obj) {
if (obj.getClass().equals(Integer.class)) {
 Toast.makeText(context, "Indeed. Working",Toast.LENGTH_SHORT).show();
}
}

sir @vknow360 have a look here once

Please do not tag users, if he gets time he will help.

Try using == operator.

still not working.

There are multiple ways to get class of an object:

Instance Method:
boolean b = o instanceof Integer; // true or false

Equals Method:
boolean c = o.getClass().equals(Integer.class); // true or false

String Method:
String s = o.getClass().toString().substring("class ".length());
String class = s.substring(s.lastIndexOf(".") + 1);

And if any of the above does not work then you should check context initialization.

Generally, if you know you're going to need an int it is advisable to use int. Kawa will automatically cast/typecheck the value to the right type. If you really want to go this route, you'll actually need to test for multiple types. You'll need to test for Integer, which is the Java boxed type for int, and you'll need to check for gnu.math.IntNum, which is Kawa's native integer type. If you're also expect the result of math operations, such as 0.5 + 0.5, then you may also want to check for gnu.math.DFloNum since the result of said operation will be 1.0, which is technically an integer but stored as a double.

1 Like

@ewpatton thanks your method works. Also thanks to @vknow360