-2

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?

steven
  • 11
  • 1
  • 2
    I doubt whether `continue` is what you meant to put inside the second `else` block. I suspect that the six lines that come after it should actually have been in the `else`, instead of the `continue`. – Dawood ibn Kareem Dec 05 '21 at 01:11
  • 1
    Also, you didn't see that error when you _compiled_ your program. You saw it when you _ran_ the program. – Dawood ibn Kareem Dec 05 '21 at 01:12
  • Does this answer your question? [What is a StringIndexOutOfBoundsException? How can I fix it?](https://stackoverflow.com/questions/40006317/what-is-a-stringindexoutofboundsexception-how-can-i-fix-it) – diggusbickus Dec 07 '21 at 00:48

2 Answers2

0

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.

shivam01-0
  • 41
  • 7
0

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.