4 Replies Latest reply: Jul 11, 2009 10:16 AM by 795748 RSS

    substring index out of range

    795748
      i am using substring
      and putting string
      into 2 jlabel's
      at runtime
      i dont know the size of index at rumtine
      though i am using database to rerieve information
      into label
      it gives me error

      String index out of  range:40_


      any solutions to this plz..........
        • 1. Re: substring index out of range
          843789
          Hi Karamjeet,

          In order to provide you some help, I suggest you to post the part of the coding where you fill 2 JLabel's.
          • 2. Re: substring index out of range
            795748
            i am using 3jlabels and putting them in jpanel
            which layout i set to gridlayout
            p1.setLayout(new GridLayout(3,0))
            and here is the problem code in which iam getting runtime error
            try
            {
            
            String txt1=res.getString(2);
            String txt01=txt1.substring(0,40);
            String txt02=txt1.substring(41);
            label1.setText(txt01);
            label3.setText(txt02);
            label2.setText(res.getString(3));
                   
                   //label4.setText(res.getString(4));
             
            }
            Edited by: Karamjeet on Jul 11, 2009 1:57 PM
            • 3. Re: substring index out of range
              843789
              You should test the length of txt1, something like this :
              String txt1=res.getString(2);
              String txt01;
              String txt02;
              if (txt1.length() > 40) {
                  txt01 = txt1.substring(0,40); // characters from 0 to 39
                  txt02 = txt1.substring(40); // characters from 40 to the end of the string
              } else {
                  txt01 = txt1;
                  txt02 = "";
              }
              • 4. Re: substring index out of range
                795748
                thanks i got it the solution
                actually i am wrapping all the text of labels in one panel
                thnks a lot