how to convert String to int

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • deepthisoft
    New Member
    • Jul 2007
    • 27

    #1

    how to convert String to int

    hai,

    I want to convert String to int.My coding is like this,

    [code=java]
    rs=stmt.execute Query("select a from mailtable");
    rs.absolute(-1);

    String id=rs.getString ("a");
    System.out.prin tln(id);

    id= id.substring(3, (id.length()));
    System.out.prin tln("value of id is "+ id);
    [/code]

    at last this id having an numeric value ,I want to convert this to int. After this i want to add 1 to that int value.How to do this please help me.

    [code=java]
    Integer.parseIn t(id);
    [/code]

    This is not working. It gives NumberFormatExc eption. Please anyone can help me.

    This is very urgent for me.
  • Nepomuk
    Recognized Expert Specialist
    • Aug 2007
    • 3111

    #2
    Originally posted by deepthisoft
    hai,

    I want to convert String to int.My coding is like this,

    [code=java]
    rs=stmt.execute Query("select a from mailtable");
    rs.absolute(-1);

    String id=rs.getString ("a");
    System.out.prin tln(id);

    id= id.substring(3, (id.length()));
    System.out.prin tln("value of id is "+ id);
    [/code]

    at last this id having an numeric value ,I want to convert this to int. After this i want to add 1 to that int value.How to do this please help me.

    [code=java]
    Integer.parseIn t(id);
    [/code]

    This is not working. It gives NumberFormatExc eption. Please anyone can help me.

    This is very urgent for me.
    If you want the ASCII value:
    [CODE=java]
    System.out.prin tln((int) id.charAt(0));
    [/CODE]
    parseInt would give you a 1 if used with the String "1".

    Comment

    • dmjpro
      Top Contributor
      • Jan 2007
      • 2476

      #3
      Originally posted by nepomuk
      If you want the ASCII value:
      [CODE=java]
      System.out.prin tln((int) id.charAt(0));
      [/CODE]
      parseInt would give you a 1 if used with the String "1".
      Look He is not Looking for ASCII Value.
      He wants to convert into Number.

      Kind regards,
      Dmjpro.

      Comment

      • Nepomuk
        Recognized Expert Specialist
        • Aug 2007
        • 3111

        #4
        Originally posted by dmjpro
        Look He is not Looking for ASCII Value.
        He wants to convert into Number.
        In that case, what output does
        [CODE=java]
        System.out.prin tln(id);
        [/CODE]
        create?
        Last edited by Nepomuk; Aug 30 '07, 10:08 AM. Reason: Had to remove some obsolete coding, which was entered automaticly

        Comment

        • dmjpro
          Top Contributor
          • Jan 2007
          • 2476

          #5
          Originally posted by deepthisoft
          id= id.substring(3, (id.length()));
          Are you sure that the id is equivalent to Number.
          Look The NumberFormatExc eption thrown when the Input String is not Equivalent to a number.
          One more thing If you want to cut a String from a Starting Index up to Last then it's enough with id.substring(3).
          Anyway, try to check that the return String whether contains any Non-Digit.
          If it is then this Exception must be thrown.
          Ok, Best of Luck with your try.

          Kind regards,
          Dmjpro.

          Comment

          • deepthisoft
            New Member
            • Jul 2007
            • 27

            #6
            Originally posted by dmjpro
            Are you sure that the id is equivalent to Number.
            Look The NumberFormatExc eption thrown when the Input String is not Equivalent to a number.
            One more thing If you want to cut a String from a Starting Index up to Last then it's enough with id.substring(3).
            Anyway, try to check that the return String whether contains any Non-Digit.
            If it is then this Exception must be thrown.
            Ok, Best of Luck with your try.

            Kind regards,
            Dmjpro.
            Thanks for your reply.

            I want to convert into integer. And then I sure it contains the value number.It is getting from the database.After three posisions it is only have numeric values only.And Also I checked that value using print statement it prints number.

            After converted into int ,I want to add 1 to that. There is any other solution for this.

            Comment

            • dmjpro
              Top Contributor
              • Jan 2007
              • 2476

              #7
              Originally posted by deepthisoft
              Thanks for your reply.

              I want to convert into integer. And then I sure it contains the value number.It is getting from the database.After three posisions it is only have numeric values only.And Also I checked that value using print statement it prints number.

              After converted into int ,I want to add 1 to that. There is any other solution for this.
              Could you send me Stack Message you got in your Console.
              I mean the total Exception Message.
              Could you sen me please?

              Kind regards,
              Dmjpro.

              Comment

              • r035198x
                MVP
                • Sep 2006
                • 13225

                #8
                Originally posted by deepthisoft
                hai,

                I want to convert String to int.My coding is like this,

                [code=java]
                rs=stmt.execute Query("select a from mailtable");
                rs.absolute(-1);

                String id=rs.getString ("a");
                System.out.prin tln(id);

                id= id.substring(3, (id.length()));
                System.out.prin tln("value of id is "+ id);
                [/code]

                at last this id having an numeric value ,I want to convert this to int. After this i want to add 1 to that int value.How to do this please help me.

                [code=java]
                Integer.parseIn t(id);
                [/code]

                This is not working. It gives NumberFormatExc eption. Please anyone can help me.

                This is very urgent for me.
                Not all Strings can be converted to integers. The string you're trying to convert to a number is not a number. That's why you got the exception.

                Comment

                • deepthisoft
                  New Member
                  • Jul 2007
                  • 27

                  #9
                  Originally posted by r035198x
                  Not all Strings can be converted to integers. The string you're trying to convert to a number is not a number. That's why you got the exception.
                  [code=java]
                  String id="EAM9";
                  id= id.substring(3, (id.length()));
                  System.out.prin tln(id);// it will print 9.
                  //after this i want to convert id into integer.
                  [/code]

                  Comment

                  • dmjpro
                    Top Contributor
                    • Jan 2007
                    • 2476

                    #10
                    Originally posted by deepthisoft
                    [code=java]
                    String id="EAM9";
                    id= id.substring(3, (id.length()));
                    System.out.prin tln(id);// it will print 9.
                    //after this i want to convert id into integer.
                    [/code]

                    I tested this Code.
                    And it works fine.

                    [code=java]
                    String id="EAM9";
                    id= id.substring(3, (id.length()));
                    System.out.prin tln("Dmjpro: " + id);
                    Sstem.out.print ln("Debasis Jana: " + Integer.parseIn t(id));
                    [/code]

                    Kind regards,
                    Dmjpro.

                    Comment

                    • JosAH
                      Recognized Expert MVP
                      • Mar 2007
                      • 11453

                      #11
                      Originally posted by deepthisoft
                      [code=java]
                      String id="EAM9";
                      id= id.substring(3, (id.length()));
                      System.out.prin tln(id);// it will print 9.
                      //after this i want to convert id into integer.
                      [/code]
                      Can you print out the *length* of that id String as well? I suspect some trailing
                      spaces or non-printing character there.

                      The example as it is runs fine for me.

                      kind regards,

                      Jos

                      Comment

                      • dmjpro
                        Top Contributor
                        • Jan 2007
                        • 2476

                        #12
                        Originally posted by JosAH
                        Can you print out the *length* of that id String as well? I suspect some trailing
                        spaces or non-printing character there.

                        The example as it is runs fine for me.

                        kind regards,

                        Jos

                        yeah Jos you are right.
                        There may be some leading or trailing spaces or non-printing Chanracter.

                        Kind regards,
                        Dmjpro.

                        Comment

                        • deepthisoft
                          New Member
                          • Jul 2007
                          • 27

                          #13
                          It is working fine.Thank you very much for all your responses.

                          Comment

                          • JosAH
                            Recognized Expert MVP
                            • Mar 2007
                            • 11453

                            #14
                            Originally posted by deepthisoft
                            It is working fine.Thank you very much for all your responses.
                            So you didn't change anything and all of a sudden it works fine? I hope you don't
                            mind if I don't believe that; care to elaborate on what you changed? It may come
                            in handy for others who are struggling with the same problem and try to google
                            for it; google might point them to this thread.

                            kind regards,

                            Jos

                            Comment

                            Working...