Home > Net >  Why am I receiving an Exception in thread "main" java.lang.StringIndexOutOfBoundsException
Why am I receiving an Exception in thread "main" java.lang.StringIndexOutOfBoundsException

Time:12-05

The problem in question is indentifying if a string has a comma and outputting the substrings from the original string.

Here is my code:

import java.util.Scanner;
import java.util.*;
import java.io.*;

public class ParseStrings {
   public static void main(String[] args) {
      
      Scanner scnr = new Scanner(System.in);
      String fullString = "";
      int checkForComma = 0;
      String firstSubstring = "";
      String secondSubstring = "";
      boolean checkForInput = false;
      
      while (!checkForInput) {
         System.out.println("Enter input string: ");
         fullString = scnr.nextLine();
         
         if (fullString.equals("q")) {
            checkForInput = true;
         }
         else {
            checkForComma = fullString.indexOf(',');
            
            if (checkForComma == -1) {
               System.out.println("Error: No comma in string");
               fullString = scnr.nextLine();
            }
            
            else {
               continue;
            }
            
            firstSubstring = fullString.substring(0, checkForComma);
            secondSubstring = fullString.substring(checkForComma   1, fullString.length());
            
            System.out.println("First word: "   firstSubstring);
            System.out.println("Second word: "   secondSubstring);
            System.out.println();
            System.out.println();
         }
         
         
      }
      
      return;
   }
   
   
}

The error I keep receiving when I compile is this:

Exception in thread "main" java.lang.StringIndexOutOfBoundsException: begin 0, end -1, length 10
at java.base/java.lang.String.checkBoundsBeginEnd(String.java:3319)
at java.base/java.lang.String.substring(String.java:1874)
at ParseStrings.main(ParseStrings.java:34)

I'm still somewhat new to programming and have never seen this type of error before, what are some ways to solve this and what might be causing it?

CodePudding user response:

The exception occurs when the index is out of range. What is a StringIndexOutOfBoundsException? How can I fix it?

For your code you are not re intializing the value of variable checkForComma

if (checkForComma == -1) 
       {
           System.out.println("Error: No comma in string");
           fullString = scnr.nextLine();
        }

If checkForComma=-1, it will then take the next input and jump to

 firstSubstring = fullString.substring(0, checkForComma);

A string index cannot be -1/negative, so it shows error.

Solution for the error You should reinatialize the value of checkForComma, as per your program intake but dont let it over pass the range of variable fullString.

CodePudding user response:

Instead of using continue in the else when u check if the checkForComma variable equals -1, u can directly use the all other code which should be running when checkForComma has a real value.

Just replace this part of the code.

            checkForComma = fullString.indexOf(',');
            
            if (checkForComma == -1) {
               System.out.println("Error: No comma in string");
               fullString = scnr.nextLine();
            }
            
            else {
               firstSubstring = fullString.substring(0, checkForComma);
               secondSubstring = fullString.substring(checkForComma   1);
            
               System.out.println("First word: "   firstSubstring);
               System.out.println("Second word: "   secondSubstring);
               System.out.println();
               System.out.println();
            }

And to get the second word, you can use only the beginning input which is checkForComma 1 in this case as this will return the value till the end of the string.

  • Related