Substring index out of bounds in java

java.lang.stringindexoutofboundsexception: string index out of range: 4
how to solve string index out of range in java
how to handle string index out of bound exception in java using try catch
substring java
java.lang.stringindexoutofboundsexception: string index out of range: 0
stringindexoutofboundsexception substring
substring out of bounds
java lang stringindexoutofboundsexception string index out of range java lang string substring

I am writing a simple code that displays only the name of the processes which are of "console" type using tasklist in java.

I am unable to do so because of the string index out of bounds error in this code. I used index 36 to 43 because in these I got the process type during output of the code where we print all the processes using tasklist. Same goes for 0 to 30 for process name.

Please help me with this.

import java.io.*;
 public class process_name
  {
    public static void main(String []args)
     {
        try {
             int i;
              String line,pn,pt;
              pn="";
              Process p = Runtime.getRuntime().exec(System.getenv("windir") +"\\system32\\"+"tasklist.exe");

              BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));

            while ((line = input.readLine()) != null)
            {
              pt=line.substring(36,43);
              if(pt.equals("Console")) 
              {
                 pn=line.substring(0,30);
                 System.out.println(pn);
              }
              System.out.println();
             }
        input.close();
       }
     catch (Exception err) 
     {
       err.printStackTrace();
     }
}

}

Just to avoid the Index to be out of bounds, I should check first if the current line contains the word "Console" and also check the length:

import java.io.*;
 public class Main
  {
    public static void main(String []args)
     {
        try {
             int i;
              String line,pn,pt;
              pn="";
              Process p = Runtime.getRuntime().exec(System.getenv("windir") +"\\system32\\"+"tasklist.exe");


              BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));

            while ((line = input.readLine()) != null)
            {
               if(line.contains("Console"))
               {
                   if(line.length()>30){
                   pn=line.substring(0,30); System.out.println(pn);}
            }
              System.out.println();
             }
        input.close();
       }
     catch (Exception err) 
     {
       err.printStackTrace();
     }
}}

String index out of bounds exception java, However, this does not go out of bounds because of the substring() "up to but not including Duration: 6:37 Posted: Nov 16, 2012 The 5 is one more than the index of the last char. However, this does not go out of bounds because of the substring() "up to but not including" use of the end index. Incidentally, the length of the resulting substring can always be computed by subtracting (end - start) -- try it with the examples above. CodingBat Practice> missingChar

Try checking the length of that line. It might not be long enough which causes an out of bounds error as it is not long enough.

 System.out.println(line.length());

or you could check the length of the line before the call

 if (line.length() >= 43){
 ....

Java Substring v2, in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: -1 at java.lang.String.substring(String.java:1931) at� The java.lang.String.substring(int beginIndex, int endIndex) method returns a new string that is a substring of this string. The substring begins at the specified beginIndex and extends to the character at index endIndex - 1. Thus the length of the substring is endIndex-beginIndex. Declaration

What I can see, tasklist prints at the beginning an empty line. An easy check would be if (!line.contains("Console")) continue; at the beginning of the while loop. With that, you skip every line, which does not contain the string Console.

How to handle the StringIndexOutOfBoundsException (unchecked , This is a classic bounds-overflow bug. Before you take a fixed-length substring or request a specific string index, you must check whether the string contains� Java String substring() method is used to get the substring of a given string based on the passed indexes. There are two variants of this method. There are two variants of this method. In this guide, we will see how to use this method with the help of examples.

How do I check if substring is out of bounds?, This program demonstrates String Index Out Of Bounds Exception in Java Exception Handling technique with sample output. You're attempting to get the character in your string, s, at index c, which is the ASCII value of the character at pos. What it seems you're intending to do is add the integer value of c to the sum, if it is, you should have

String Index Out Of Bounds Exception Java Example Program, In Java, “String index out of range” exception is thrown in java substring() & charAt() method. Here, we see the exception “java.lang. Java String indexOf() The java string indexOf() method returns index of given character value or substring. If it is not found, it returns -1. The index counter starts from zero.

String index out of range – Yawin Tutor, If you have a error of type “out of range” is because you want e.g. the last char (g in the example) and you try to get it like: Why am I getting array index out of bounds exception? try to access/change value which is out of the index (value > 4) java raise error (Index out of range) How do you find the substring of a string? Java substring: 'string index out of range' Ask Question Asked 11 years, 1 month ago. Active 1 year, 3 months ago. Viewed 286k times 25. 4. I'm guessing

Comments
  • can you print the line in console of editor and see if the line is expected, and also do you have empty line in the file that you are reading, did you cross check that?
  • You may use regex, seems that you're line is just not so long, print it, and you'll find
  • The first line of the tasklist output is empty, just skip the first line or check if the line is not empty