Results 1 to 7 of 7
  1. #1
    Ajitha is offline Member
    Join Date
    May 2010
    Posts
    17
    Rep Power
    0

    Default Help me to understand substring() method

    Hi,

    I need to get processID from command prompt for running process. Value is displayed, but I got StringIndexOutOfBoundsException during run time. I don't know where 'm wrong using substring() method. Can any one please help me in this regard.

    My code is:
    Java Code:
     String cmd = "tasklist";
                Runtime rt = Runtime.getRuntime();
                Process p = rt.exec(cmd);
                BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
                String line = null;
               while ((line = reader.readLine()) != null) {
                    if (line.indexOf("udefrag.exe") >= 0) {
                            // System.out.println(line);
                            StringTokenizer st = new StringTokenizer(line," ");
                            while (st.hasMoreTokens()) {
                                String type = (String) st.nextElement();
                                value = (String) st.nextElement();
                                System.out.println("TYPE::" + type);
                                System.out.println("VALUE::" + value);
                                String size = value.substring(0, value.indexOf('C')-1);
                                System.out.println("Size:"+size);
                      }
                   }
               }
    Output:
    Java Code:
    TYPE::udefrag.exe
    VALUE::2896
    java.lang.StringIndexOutOfBoundsException: String index out of range: -2
            at java.lang.String.substring(String.java:1937)
            at pcpromoterapp.taskKill.getProcessID(taskKill.java:37)
            at pcpromoterapp.taskKill.main(taskKill.java:55)
    Thanks in advance..

    Thanks,
    Ajitha.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default

    got StringIndexOutOfBoundsException
    substring(0, value.indexOf('C')-1);
    What is the value of: value.indexOf('C')-1 ??
    Change your code to test that indexOf() finds the value before using it.

  3. #3
    Ajitha is offline Member
    Join Date
    May 2010
    Posts
    17
    Rep Power
    0

    Default

    Hi,
    I verified what value i got using indexOf().

    If i use
    value.indexOf('C'); it returns -1 and

    value.indexOf('C')-1; returns 0

  4. #4
    Ajitha is offline Member
    Join Date
    May 2010
    Posts
    17
    Rep Power
    0

    Default

    Hi,

    Sorry, for value.indexOf('C'); it returns -1 and

    value.indexOf('C')-1; it returns -2

    Thanks,
    Ajitha

  5. #5
    Ajitha is offline Member
    Join Date
    May 2010
    Posts
    17
    Rep Power
    0

    Default

    Hi,

    I got solution. Use of substring is not required in my case, i just add break in my code and it works fine for me.


    Thanks,
    Ajitha

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default

    Read the API doc. What does a returned value of -1 mean????

  7. #7
    Ajitha is offline Member
    Join Date
    May 2010
    Posts
    17
    Rep Power
    0

    Default

    Hi,

    When the string or character specified in indexOf() method method is not present, then it returns -1 value.


    Regards,
    Ajitha.

Similar Threads

  1. [SOLVED] Substring Method
    By javanewbie in forum New To Java
    Replies: 1
    Last Post: 06-16-2009, 09:24 AM
  2. Help me understand this method
    By VinceGuad in forum New To Java
    Replies: 4
    Last Post: 04-11-2009, 01:00 AM
  3. Reverse a string not using the substring method
    By kathyla18 in forum New To Java
    Replies: 17
    Last Post: 04-08-2009, 04:08 AM
  4. Replies: 3
    Last Post: 10-06-2008, 11:03 PM
  5. How to use subString method
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-17-2008, 07:44 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •