Converting a string to an integer on Android
See the Integer class and the static parseInt()
method:
http://developer.android.com/reference/java/lang/Integer.html
Integer.parseInt(et.getText().toString());
You will need to catch NumberFormatException
though in case of problems whilst parsing, so:
int myNum = 0;
try {
myNum = Integer.parseInt(et.getText().toString());
} catch(NumberFormatException nfe) {
System.out.println("Could not parse " + nfe);
}
How to convert string to integer in android?
EDIT:
if(userid != null && !userid.trim().isEmpty()){
int U_ID = Integer.parseInt(userid)
}
Converting string into int to find a result in java android
Instead of Integer.parseInt(getTextView);
, you will have to first extract the numbers from the string obtained from the TextView
and then convert them to integer separately and then do the arithmetic operation.
Do it as below.
equal.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String getTextView = textView.getText().toString();
String[] numbers = getTextView.split("+");
int value = Integer.parseInt(numbers[0]) + Integer.parseInt(numbers[1]);
textView.setText(value);
}
}
UPDATE
Replace
String[] numbers = getTextView.split("+");
With
String[] numbers = getTextView.split("\\+");
to prevent the dangling metacharacter error.
How to convert an image string into integer on Android?
Let us assume you are trying to display the image which is in Base64
format.
Decode the incoming Base64
string to ByteArray
val imageByteArray = Base64.decode(user.image, Base64.DEFAULT)
Then load imageByteArray
to the Glide
as below.
Glide
.with(this)
.asBitmap()
.load(imageByteArray) // ==> Pass the ByteArray here
.into(nav_user_image)
EDIT 1 : Updated Answer
From your code I can see that, drawing to canvas code is placed outside the addOnSuccessListener
from snapshots
. But you are accessing the fields of loggedInUser
before the completion of addOnSuccessListener
where the data is assigned.
addOnSuccessListener
isasync
operation and will not be executed
in sequence like you written in your existing code.
Move the following code inside the addOnSuccessListener
.
canvas1.drawBitmap(BitmapFactory.decodeResource(resources,loggedInUser.image.toInt()), 0F, 0F, color)
canvas1.drawText("User Name!", 30F, 40F, color)
Your existing code.
mFireStore.collection(Constants.USERS)
.document(FirestoreClass().getCurrentUserId())
.get()
.addOnSuccessListener { document ->
loggedInUser = document.toObject(User::class.java)!!
}
canvas1.drawBitmap(BitmapFactory.decodeResource(resources,loggedInUser.image.toInt()), 0F, 0F, color)
canvas1.drawText("User Name!", 30F, 40F, color)
Updated Answer 2 :
Based on the error log, the image is retrieved as an remote url which can be directly loaded in the ImageView
. But when you are trying to draw it in the canvas
you need to decode the file from the remote url as stream
and then set it in the Canvas
.
Something like below
URL url = new URL(<PASS YOUR URL from FIREBASE>);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
....
Bitmap receivedImageasBitmap = BitmapFactory.decodeStream(connection .getInputStream());
..
canvas1.drawBitmap(receivedImageasBitmap)
Converting String to Int in Android, JSON
As simple as:
int i = Integer.parseInt(yourString);
Or, for floats:
float f = Float.parseFloat(yourString);
Converting String/Double to Int in Android
double x = Double.parseDouble(your_string);
int y = (int) x;
y is going to have value of x with decimals cutted of.
Before casting to int you are able to floor or ceil the number if you want.
Don't forget that parsing functions usually throw an exception when your_string is not a number.
android studio how to convert string with id to int?
What you are asking is completely wrong.
"R.id.id"
is a String
that can't be converted in a int.
It will always throw a NumberFormatException
.
Check the javadoc:
Parses the string argument as a signed decimal integer. The characters in the string must all be decimal digits, except that the first character may be an ASCII minus sign '-' ('\u002D') to indicate a negative value or an ASCII plus sign '+' ('\u002B') to indicate a positive value. The resulting integer value is returned, exactly as if the argument and the radix 10 were given as arguments to the parseInt(java.lang.String, int) method.
If the resource used in your strings.xml
with the same id
is a integer, you have to use somenthing different:
<resources>
<string name="mystring">12</string>
</resources>
Use this code to retrieve a string:
String string = getString(R.string.mystring);
try{
int number = Integer.parseInt(string);
} catch (NumberFormatException nfe){
//.....
}
How to convert String to Int in Kotlin?
You could call toInt()
on your String
instances:
fun main(args: Array<String>) {
for (str in args) {
try {
val parsedInt = str.toInt()
println("The parsed int is $parsedInt")
} catch (nfe: NumberFormatException) {
// not a valid int
}
}
}
Or toIntOrNull()
as an alternative:
for (str in args) {
val parsedInt = str.toIntOrNull()
if (parsedInt != null) {
println("The parsed int is $parsedInt")
} else {
// not a valid int
}
}
If you don't care about the invalid values, then you could combine toIntOrNull()
with the safe call operator and a scope function, for example:
for (str in args) {
str.toIntOrNull()?.let {
println("The parsed int is $it")
}
}
Converting a String to an Integer in Android
Set wallpaper from URL
try {
URL url = new URL("http://developer.android.com/assets/images/dac_logo.png");
Bitmap bitmap = BitmapFactory.decodeStream(url.openStream());
wallpaperManager.setBitmap(bitmap);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Enjoy =)
Related Topics
Recyclerview Item Click Listener the Right Way
Check Group Membership for a Linux User Using Java
Compute Hex Color Code for an Arbitrary String
Convert Character to Ascii Numeric Value in Java
Java Count Occurrence of Each Item in an Array
Hibernate Criteria Returns Children Multiple Times with Fetchtype.Eager
Read Content from Files Which Are Inside Zip File
Differencebetween Compare() and Compareto()
How to "Intercept" Ctrl+C in a Cli Application
Why Double Width = 50/110000; the Output Is 0.000000000000000
What Is Shareduserid in Android, and How Is It Used
Why Does "String".Getbytes() Work Different According to the Operation System
Jsp, Can It Work Similar to Yield, Layout, Content_For in Ruby/Rails/Erb
Recommended Jsf 2.0 Crud Frameworks
How to Hash Some String with Sha256 in Java