Strings in AWT

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • shilpa handoo
    New Member
    • Mar 2008
    • 3

    Strings in AWT

    I would like to know a method for converting a string in int array for doing some
    manipulations on array.

    i.e i am retrieving a string from awt's textfield .
    i want to convert that into an array and calculate sum of even nos and
    odd nos seperately.

    waiting for reply

    thanks
    shilpa
  • myusernotyours
    New Member
    • Nov 2007
    • 188

    #2
    You cannot convert the string in to an array. You can only convert it into an int then store the int into an array. Use the integer wrapper class - Integer, and the static method - parseInt. The method receives a string parameter and returns an int.

    regards.

    Comment

    • 6502kid
      New Member
      • Mar 2008
      • 6

      #3
      Study this jsp snippet:

      Code:
      <%
                int strSum = 0;
                String tmpLine = "122212";  // put your AWT string here
                int i = 0;
                String tmpc = "";
      
                   while (i < tmpLine.length()) {
                   tmpc = tmpc + tmpLine.charAt(i);
                   strSum = (strSum + Integer.parseInt(tmpc));
                   i++; tmpc = "";
                   }
      
      %>
      Sum of tmpLine = <%= strSum %>
      You should be able to modify the above to either return an array of
      integers, or work out the odd/even thing within the loop.

      Comment

      • shilpa handoo
        New Member
        • Mar 2008
        • 3

        #4
        Thanx a lot

        regards

        shilpa







        Originally posted by myusernotyours
        You cannot convert the string in to an array. You can only convert it into an int then store the int into an array. Use the integer wrapper class - Integer, and the static method - parseInt. The method receives a string parameter and returns an int.

        regards.

        Comment

        • shilpa handoo
          New Member
          • Mar 2008
          • 3

          #5
          Thanx a lot.

          Your sited example has helped me in solving my problem.

          Regards

          shilpa



          Originally posted by 6502kid
          Study this jsp snippet:

          Code:
          <%
                    int strSum = 0;
                    String tmpLine = "122212";  // put your AWT string here
                    int i = 0;
                    String tmpc = "";
          
                       while (i < tmpLine.length()) {
                       tmpc = tmpc + tmpLine.charAt(i);
                       strSum = (strSum + Integer.parseInt(tmpc));
                       i++; tmpc = "";
                       }
          
          %>
          Sum of tmpLine = <%= strSum %>
          You should be able to modify the above to either return an array of
          integers, or work out the odd/even thing within the loop.

          Comment

          Working...