10 Replies Latest reply: Oct 9, 2006 1:12 PM by 807598 RSS

    how to conver object into int

    807598
      Hi techies,
      I am trying to conver object into String[].I tried with the following code. But I am having some other code rather than integer
      Here is my code
      String temp =session.getAttribute("ram").toString();
                      int kk =0;
                     kk= Integer.parseInt(temp);
      am I missing anything in my code


      regards,
      Krish
        • 1. Re: how to conver object into int
          807598
          I don't understand your question. Your subject says you want to convert an Object into an int and your code does that as well but then in your question you say something about converting to a String array.

          What are you trying to do?
          • 2. Re: how to conver object into int
            807598
            I am getting Ljava.lang.String;@17da7f4" . But I need Integer value and also i want to know how can I convert it into String[]
            • 3. Re: how to conver object into int
              807598
              I am getting Ljava.lang.String;@17da7f4" . But I
              need Integer value and also i want to know how can I
              convert it into String[]
              That is an array of Strings. Which one represents the int that you want? Do you know?
              • 4. Re: how to conver object into int
                DrClap
                To get that array of strings in a form your program can use, cast it:
                String[] theArray = (String[]) session.getAttribute("ram");
                • 5. Re: how to conver object into int
                  807598
                  Im guessing you are getting some this data out of somthingl ike an HttpServletSession or somthing like that. I dont understand what you mean by "But I need Integer value and also i want to know how can I convert it into String[]". Do you mean you want to get the int value of that attribute, or do you mean you want the Integer value (im gussing the former). And I have no clue what you are asking about the string[]. Take a second and try to formulate your question. You have to remeber we have no clue what you are talking about. As for converting that to an integer try this:
                  int temp = Integer.parseInt((String)session.getAttribute("ram"));
                  I dont think that toString method gives you the correct value.
                  • 6. Re: how to conver object into int
                    807598
                    How to can I convert object into String[]. For xample here I am having like this .
                    session.getAttribute("ram").
                    Then How can I convert into String[]
                    • 7. Re: how to conver object into int
                      807598
                      How to can I convert object into String[]. For
                      xample here I am having like this .
                      session.getAttribute("ram").
                      Then How can I convert into String[]
                      It IS a String[]. Please read reply 5.

                      You really should consider the oft given advice to you to go through some basic Java tutorials. 300 plus posts and utter confusion about basics in all of them.
                      • 8. Re: how to conver object into int
                        807598
                        As for converting that to an integer try this:
                        int temp = Integer.parseInt((String)session.getAttribute("ram"));
                        I dont think that toString method gives you the correct value.
                        If casting to a String works (doesn't give ClassCastException), then toString will give the same answer (toString on a String just gives back the String itself).
                        • 9. how to conver object into int
                          807598
                          i have tried to treemap a string and an integer as shown below...

                          TreeMap am=new TreeMap();
                          am.put("a",new Integer(1));
                                    am.put("b",new Integer(2));
                                    am.put("c",new Integer(3));
                                    am.put("d",new Integer(4));

                          and many others too....

                          now using the get function i wnt to retrieve the values of the a,b,cor d.these alpahbets(strinsi.e a,b,c,d) are stored in an array

                          so wen i tried this using the followin....

                          for(int i=0;i<length;i++)
                                    {
                          int b=(am.get(a))
                          }


                          but its givin me an error......
                          im not able to copy the value into b..which should have been b=1 if a[i]=a
                          • 10. Re: how to conver object into int
                            807598
                            i have tried to treemap a string and an integer as
                            shown below...

                            TreeMap am=new TreeMap();
                            am.put("a",new Integer(1));
                            w Integer(2));
                                      am.put("c",new Integer(3));
                                      am.put("d",new Integer(4));

                            and many others too....

                            now using the get function i wnt to retrieve the
                            values of the a,b,cor d.these alpahbets(strinsi.e
                            a,b,c,d) are stored in an array

                            so wen i tried this using the followin....
                            > for(int i=0;i<length;i++)
                            
                                      {
                            int b=(am.get(a))
                            }


                            but its givin me an error......
                            im not able to copy the value into b..which should
                            have been b=1 if a[ i]=a

                            You need:
                            int b = ((Integer)am.get(a)).intValue();

                            Please use code tags (see button above posting box), so you don't get italics from your array index. Also, this thread that you resurrected isn't really related--you could have started your own thread. It's good to know that you were searching for an answer before posting, though. :)

                            By the way, you should use better variable names than 'a' and 'b'.