changeweek = (Map<String,ArrayList<Integer>>)dataSnapshot.child("week").getValue();
ArrayList<Integer> test = changeweek.get("Monday");
Log.d("changeweek",changeweek.toString());
int j = test.get(2);
I get an error in the last line which is the following:
java.lang.ClassCastException: java.lang.Long cannot be cast to java.lang.Integer
at com.example.fake9.tendee.ScheduleActivity$1$1.onDataChange(ScheduleActivity.java:107)
I don't know how this happens since I am storing Arraylist of integers into the hashmap. The following is a picture of the database.
The Firebase SDK internally stores all integer-like number values as Long values, whether or not you want. This helps defend against possibly very large numbers as values.
Your cast to a Map with values of type Integer is overriding that, then causing problems at runtime when the types don't match. You can correct this by simply changing your value type from Integer to Long.
Rather than directly converting long to int, convert long to string using String.valueOf() then we can easily convert string value to int using Integer.parseInt()
So you can go with this,
**
int j = Integer.parseInt(String.valueOf(test.get(2)));
**
Related
I can get any type from my Firestore DocumentSnapshot except for Int:
Why is this?
As you can see from the documentation, Cloud Firestore's integral type values are 64 bit signed, which means you would need a JVM long to hold it without possibly losing data. If you really must store an integral number as a JVM int type, you should just cast the value obtained from getLong() to an int.
Using kotlin Int class will do
val user1MinAge = dataSnapshot.getValue(Int::class.java)
I have an object/list coming from parse.com(using parse sdk) and receive both strings and numbers. While I am able to fetch strings I don't know how to fetch numbers.
This works fine for string as driver:
ParseObject u = (ParseObject)scoreList.get(i);
String truckName;
truckName = u.getString("driver").toString();
How do I get the number assuming my "xcor" (assume another property such as "driver") is a number not string.
Maybe I don't understand your question perfectly, but isn't this:
double xcor = u.getDouble("xcor");
just what you need?
(alternatively if you store integer instead of a double you can use int otherVariable = u.getInt("otherVariable");)
I'm currently developing a math application that makes long computations. I'm getting java.lang.NumberFormatException: Invalid int: "..." error (where the ... is replaced by a very long number) whenever I type an integer that contains more than 9 digits. When I type in an integer that is less than or equal to 9 digits, the application runs fine. I need the output to be an int (i.e. no decimal places). Not quite sure why the error's occurring.
The bit of code that's causing the problem is:
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.NUMBER);
int inp = Integer.parseInt(message);
The maximum value for an int is 231-1, i.e. 2,147,483,647. If you try to parse a larger number than that, the exception will be thrown.
If you need to handle larger numbers, either use long for a generally larger range (up to 263-1) or BigInteger for an arbitrary size.
Java's int datatype is limited to the values of -2,147,483,648 to 2,147,483,647 (inclusive). If you want larger 'integer' values I'd recommend using the long datatype.
I am making an android application where i got a set of strings that i load from SharedPreferences so that i can save the strings. The strings contain only numbers, but it is not an int value, its a string value. And i wounder how i can minus the numbers that's in there, becuase usually, i would have been using something like an int value = value - value; But that doesn't seem to work since it's a string and not an int value. How can i do this even though it's a string? I know i could use int values instead, but as i didn't think of this before now, when i'm almost done, it would be alot of work changing all of the code that's related to this. Please help me and thanks so much in advance!
You will have to convert your strings to ints first, then operate on them, then save the string back:
String value = preferences.getString("key:");
int intValue = Integer.valueOf(value);
intValue = intValue - 1;
preferences.edit().putString("key", Integer.toString(intValue)).commit();
Try using Integer.valueOf(string) or Integer.parseInt(string).
Learning basic programming the the concept of casting will help you tremendously. One datatype can be converted to another using the base classes, which often times deal with String. For instance look at the Documentation of Integer.
Well as you have said that it is a lot of work to change the code and save it as int, I would suggest converting the string into an integer, refer to this link for more information, someone has asked about converting strings to integers, and as Android is Java-based, this can apply to your project:
Converting a string to an integer on Android
Hope this helps.
Why does this code trigger a force close in Android?
`score.setText(Integer.parseInt((String) score.getText())+1);`
score is a TextView, and I am simply increasing the number by 1. I have predefined a String resource to be the initial number in the score TextView.
I am quite frustrated.
First off you should try breaking down your code so you can actually see what is going on with it.
Instead of
score.setText(Integer.parseInt((String) score.getText())+1);
try
String tmp = score.getText().toString();
int score;
score = Integer.parseInt(tmp) + 1;
score.setText(String.valueOf(score));
EDIT: Upon further reading of the documentation, setText has several overloads, one of which DOES take an int, but it takes the int of a resource ID. My guess is that your score is not a valid resource ID, thus crashing your application.
public final void setText (int resid)
Oh and as far as the frequent FC's when beginning Android Dev, it happens to the best of us. The key is to learn WHY the FC's happen, and have a LOT of patience.
mostly u need to do this
score.setText(Integer.parseInt(score.getText().toString())+1);
coz.. getText() returns a Editable Object which cannot be parsed to Integer. So it give NumberFormat Exception.
AndMake sure to set TextView,s Text to an integer initially..
try this way
score.setText(String.valueOf(Integer.parseInt(score.getText().toString())+1));
as you can pass the integer value that's why getting force the application
TextEdit.setText takes a CharSequence as input.
You are supplying an integer through Integer.parseInt((String) score.getText())+1
See, if converting it back to string and using it in setText helps.
You can convert an integer to string using Integer.toString.
PS: I am new to java myself.
The compiler should have ideally caught this error.
It's possible java uses some implicit type conversions from string to int.