Why cannot convert from string to int?

convert string to int java
convert string to int javascript
int cannot be converted to string
convert string to int in c
convert string to int c++
convert string to int python
convert string to int php
convert string to int javatpoint

It should be something easy to do, but these commands return error when I try to convert a String to integer:

        Intent recibir = getIntent();
        String nombre = recibir.getStringExtra("hora_inicio");

        int numero=0;

        try {
            numero = Integer.parseInt(nombre);
        } catch(NumberFormatException nfe) {

        Toast.makeText(this, numero, Toast.LENGTH_SHORT).show();

Android monitor returns this error:

E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.cristobal.policlinica, PID: 11025 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.cristobal.policlinica/com.example.cristobal.policlinica.CalendarActivity}: android.content.res.Resources$NotFoundException: String resource ID 0x9 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) at android.app.ActivityThread.-wrap11(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:148) at android.app.ActivityThread.main(ActivityThread.java:5417) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) Caused by: android.content.res.Resources$NotFoundException: String resource 0x9 at android.content.res.Resources.getText(Resources.java:312) at android.widget.Toast.makeText(Toast.java:286) at com.example.cristobal.policlinica.CalendarActivity.onCreate(CalendarActivity.java:31) at android.app.Activity.performCreate(Activity.java:6237) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)  at android.app.ActivityThread.-wrap11(ActivityThread.java)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)  at android.os.Handler.dispatchMessage(Handler.java:102)  at android.os.Looper.loop(Looper.java:148)  at android.app.ActivityThread.main(ActivityThread.java:5417)  at java.lang.reflect.Method.invoke(Native Method)  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

thanks in advance

The problem is not with your conversion but with your Toast.makeText. When you call Toast.makeText(this, numero, Toast.LENGTH_SHORT).show();

It considers numero as resId and tries to find the relative string resource in strings.xml which fails giving error:

Resources$NotFoundException: String resource ID

Instead of

Toast.makeText(this, numero, Toast.LENGTH_SHORT).show();


Toast.makeText(this, String.valueOf(numero), Toast.LENGTH_SHORT).show();

How to convert a Java String to an Int, Answer: Convert a string to an integer with the parseInt method of the Java Integer class. The parseInt method is to convert the String to an int and throws a NumberFormatException if the string cannot be converted to an int type. Only works with valid integer string. Input string must be within the range of called IntXX method e.g. Int16, Int32, Int64. The input string cannot include parenthesis, comma, etc. Must use a different method for different integer ranges e.g. cannot use the Convert.ToInt16() for the integer string higher than "32767".

Try this:

 number = Integer.parseInt(YourStringName.toString());

How to convert string to int in C#?, Learn how to convert a string to an integer in C# using different methods and Input string must be valid number string, cannot include different  I have a TextBoxD1.Text and I want to convert it to an int to store it in a database. How can I do this?

Toast.makeText(this, String.valueOf(numero), Toast.LENGTH_SHORT).show();

Java – Convert String to int – Mkyong.com, Example to convert a String “10” to an primitive int. In summary, parseInt(String) returns a primitive int, whereas gives you an error you cant assign such a big number, you should assign it to 64 bit int, or leave it as a String. The .NET Framework provides a couple built in ways to convert a C# string to int, or several other numeric datatypes. It is important that you do so in a way that does not throw potential exceptions. Unsafe Ways to Convert a C# String to Int. You can use a couple built in methods, as shown below, to convert a string to int.

How to convert a Java String to an Int, The most direct solution to convert a string to an integer is to use the a NumberFormatException if the string can't be converted to an int type. Looking at converting a string to an int and I don't mean ASCII codes. For a quick run-down, we are passed in an equation as a string. We are to break it down, format it correctly and solve the linear equations. Now, in saying that, I'm not able to convert a string to an int.

Java Integer parseInt() method with Examples, How do I convert a string to an int in C#? It is common to convert string to int and int to string in C++ programs. This post introduces how to convert a valid string to int and int to string in C++ using C and C++ ways and libraries. If your string is not a valid integer string, you should check it first or use other methods.

How to convert a string into integer in JavaScript?, This method returns the integer value which is represented by the argument in decimal equivalent. Using SQL to convert a string to an int is used in a variety of situations. You may have text data that you cannot alter at the source and you need to get some accurate answers from it. You may also have text data that you want to insert to an integer column. There could be other reasons too.

  • check this Resources$NotFoundException: String resource ID And set Toast.makeText(this, ""+numero, Toast.LENGTH_SHORT).show();
  • check if(!nombre.isempty) then convert it to int
  • Toast.makeText(this, String.valueOf(numero), Toast.LENGTH_SHORT).show();
  • Share your code where you are setting intent data.
  • As an opinion, String.valueOf(numero); is more efficient and elegant
  • @MarcEstrada yeah, great idea. Updated the answer
  • And voted your answer :) there is a lot of lates answer saying same of you, so voted yours
  • @MarcEstrada Thanks!
  • @Mimmetico great! Remember to mark answer as approved, so that others can benefit too