Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Java
  4. convert to integer?

convert to integer?

Scheduled Pinned Locked Moved Java
questionhelp
12 Posts 8 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P pancakeleh

    i have this particular line in my program however when i execute this line it gives me error. Integer addIncidentID = (Integer) getIncidentidtxt().getValue(); how do i convert from string to integer?

    T Offline
    T Offline
    TorstenH
    wrote on last edited by
    #3

    this is a static method of the type Integer:

    int iValue = Integer.parseInt(givenString);

    This one throws an exception when converting fails. So best is to wrap in a try/catch:

    int iValue=0; // declared and initialized to a simple value (could be -1 when 0 is used)
    try{
    iValue = Integer.parseInt(givenString);
    }
    catch(NumberFormatException oException){
    // doShowFunnyPopup();
    }

    regards, Torsten

    I never finish anyth...

    1 Reply Last reply
    0
    • P pancakeleh

      i have this particular line in my program however when i execute this line it gives me error. Integer addIncidentID = (Integer) getIncidentidtxt().getValue(); how do i convert from string to integer?

      P Offline
      P Offline
      pancakeleh
      wrote on last edited by
      #4

      can i use interger.parseint?

      T C 2 Replies Last reply
      0
      • P pancakeleh

        can i use interger.parseint?

        T Offline
        T Offline
        TorstenH
        wrote on last edited by
        #5

        that's kind of the one I mentioned - if "interger" is just a typo and is supposed to be "Integer". see post above your post. regards Torsten

        I never finish anyth...

        1 Reply Last reply
        0
        • P pancakeleh

          can i use interger.parseint?

          C Offline
          C Offline
          Cedric Moonen
          wrote on last edited by
          #6

          Please, check the javadoc ! Yes you can use parseInt but it returns an int, not an Integer. So, depending on your needs, either use valueOf or parseInt.

          Cédric Moonen Software developer
          Charting control [v3.0] OpenGL game tutorial in C++

          1 Reply Last reply
          0
          • P pancakeleh

            i have this particular line in my program however when i execute this line it gives me error. Integer addIncidentID = (Integer) getIncidentidtxt().getValue(); how do i convert from string to integer?

            R Offline
            R Offline
            RaviRanjanKr
            wrote on last edited by
            #7

            pancakeleh wrote:

            how do i convert from string to integer?

            you can convert string to integer by using various ways you can use valueOf method of Integer class in this way

            Integer obj = Integer.valueOf(str_value); // str_value is your string value

            you can use Integer Constructor

            Integer obj = new Integer(str_value);

            you can use parseInt method

            int objt = Integer.parseInt(str_value);

            for more details move there[^]

            1 Reply Last reply
            0
            • P pancakeleh

              i have this particular line in my program however when i execute this line it gives me error. Integer addIncidentID = (Integer) getIncidentidtxt().getValue(); how do i convert from string to integer?

              V Offline
              V Offline
              vasehu
              wrote on last edited by
              #8

              For Example: String abc123 = "123"; Integer abcint = Integer.valueOf(abc123);

              P 1 Reply Last reply
              0
              • V vasehu

                For Example: String abc123 = "123"; Integer abcint = Integer.valueOf(abc123);

                P Offline
                P Offline
                pancakeleh
                wrote on last edited by
                #9

                i got it. thanks alot.

                1 Reply Last reply
                0
                • C Cedric Moonen

                  Of course you can't cast a String to an Integer, they are two completely unrelated types. You have to use the Integer.valueOff(String s) method:

                  Integer addIncidentID = Integer.valueOff(getIncidentidtxt().getValue());

                  Cédric Moonen Software developer
                  Charting control [v3.0] OpenGL game tutorial in C++

                  U Offline
                  U Offline
                  User 4503017
                  wrote on last edited by
                  #10

                  // to primitive int i from Integer Object ii int i = ii.intValue(); // to Integer ii from primitive int i Integer ii = new Integer( i );

                  1 Reply Last reply
                  0
                  • P pancakeleh

                    i have this particular line in my program however when i execute this line it gives me error. Integer addIncidentID = (Integer) getIncidentidtxt().getValue(); how do i convert from string to integer?

                    B Offline
                    B Offline
                    bilal hobnail
                    wrote on last edited by
                    #11

                    int int_page_id=o; String str_page_id="5"; try { int_page_id = Integer.parseInt(str_page_id); } catch(NumberFormatException ex) { ex.printStackTrace(); }

                    1 Reply Last reply
                    0
                    • P pancakeleh

                      i have this particular line in my program however when i execute this line it gives me error. Integer addIncidentID = (Integer) getIncidentidtxt().getValue(); how do i convert from string to integer?

                      R Offline
                      R Offline
                      Ramaiah Raj
                      wrote on last edited by
                      #12

                      String a="2"; int value = Integer.parseInt(a);

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups