• Register
0 votes
440 views

Problem :

I am new to string handling & getting below error while trying to execute a program
java.lang.stringindexoutofboundsexception: string index out of range: 0
6.9k points

Please log in or register to answer this question.

2 Answers

0 votes

Solution :

This problem generally occurs when any line is empty . Then it will not find any character at index 0 so the error is thrown.

To fix the issue, one can check the length of line before one uses charAt:

System.out.println(currentline);
char y;
if (currentline.length() < 1) {
   System.out.println("the current line is empty");
} else {
    y = currentline.charAt(0);

    while((currentline.charAt(0)!='/')&&(Character.isWhitespace(y)==false))
    {
       currentline = inputFile.nextLine();
       if (currentline.length() < 1) {
          System.out.println("current line is empty");
          break;
       }
       y = currentline.charAt(0);
       System.out.println(currentline);
       System.out.println(y);
    }
}

Further Readings:

https://github.com/vert-x3/issues/issues/329

38.6k points
edited by
0 votes

Solution:

The problem happens at the time line is empty (for example ""). Then it doesn't have a character at index 0, therefore your error.

To solve, you can inspect the length of line prior you employ charAt:

System.out.println(line);
char x;
if (line.length() < 1) {
   System.out.println("next line is empty");
} else {
    x = line.charAt(0);

    while((line.charAt(0)!='/')&&(Character.isWhitespace(x)==false))
    {
       line = inputFile.nextLine();
       if (line.length() < 1) {
          System.out.println("next line is empty");
          break;
       }
       x = line.charAt(0);
       System.out.println(line);
       System.out.println(x);
    }
}

Appears like line=="" , first check if the line String has some content and then attempt to invoke charAt() .

if(line!=null && line.length()>0) // perform this check to be safe.

First of all, congrats on getting commenced on Java. It's an amazing language and the possibilities are endless. Here is how you can go about solving your error.

The first step is to understand accurately what your error implies, so let's get to that.

java.lang.StringIndexOutOfBoundsException: String index out of range: 1

The type of exception thrown is a StringIndexOutOfBoundsException. Anytime you obtain an IndexOutOfBoundsException (or any type thereof) it implies that you are attempting to access an index in an array that doesn't subsist. By calling the substring system, you are dividing the string in a character array, and chooosing characters A to B (In your case 0 to 1). In case character 1 doesn't subsist, and else you attempt to access it, it will throw this error.

The cause you are obtaining this error is hence that you are attempting to execute a substring(0,1) on a String with less than 1 character, aka an empty string or even a null string maybe.

Further, to provide you a quick tip. Generally, a you must make a new method in case your method body extends 12 lines. This will hugely rise the readability of your code. It took me a while to trace the right place for your fix :

Change

if (mots.get(j).startsWith(searchPhrase.substring(0, 1))) {

to

if (searchPhrase != null && searchPhrase.length() > 0 && mots.get(j).startsWith(searchPhrase.substring(0, 1))) {

By doing this you check in case the length of your string is sufficient prior executing the substring method.

31.7k points
edited by

Related questions

0 votes
1 answer 44 views
44 views
Problem: I have written code for exception. It's building a file but not letting it run, it just throws exception This is code : public static void main(String args[]) { String name = "Umer Hassan"; String name1 = "Hassan Umer"; char[] name2 = new ... at Anagram.main(Anagram.java:24)exception in thread "main" java.lang.stringindexoutofboundsexception: string index out of range: 11, Why is that???
asked Apr 26 nabin 5.1k points
0 votes
1 answer 49 views
49 views
Problem: The program checks for sentences without spaces to extract words which matches the dictionary of valid English words import java.util.ArrayList; import java.util.Arrays; import java.util.List; class WordCheckUtil{ boolean ... WordCheckUtil.wordBreakCheck(WrodCheckUtil.java:37) at WordCheckUtil.wordCheck(WrodCheckUtil.java:20) at WordCheckUtil.main(WrodCheckUtil.java:44)
asked May 1 anika11 32.2k points
0 votes
1 answer 40 views
40 views
Problem: java lang stringindexoutofboundsexception string index out of range 0
asked Feb 23 Hira 2.2k points
0 votes
1 answer 12 views
12 views
Problem: What exactly is the solution : Java.lang.arrayindexoutofboundsexception: length=0; index=0?
asked Jun 22 Nahil 128k points
0 votes
1 answer 52 views
52 views
I need to find frequency of charts in a string? can anyone here help me?
asked Sep 22, 2020 Daniel Anderson 4k points
0 votes
1 answer 1.6k views
1.6k views
Problem : I want to input some of the details about student to my program. But when I press a save button its giving me the below listed exception error: "Exception in thread "AWT-EventQueue-0" java.lang.UnsupportedOperationException: Not supported yet."
asked Jan 31, 2020 jwilliam 3.9k points
0 votes
1 answer 35 views
35 views
Problem: I need someone&rsquo;s help to fix it, out of range exception c++
asked Mar 22 Ifra 43.4k points
1 vote
1 answer 179 views
179 views
Problem : I am a Computer Science student. I am trying to learn Java Programming in this Covid19 lockdown period. I want to write a program which should perform the below task. “Write a java program to count number of characters in given string”. Please let me know the exact procedure to write the above mentioned program.
asked May 19, 2020 Martin K 6.6k points
0 votes
1 answer 35 views
35 views
Problem: Working on this, however have no luck, need help : Exception in thread "awt-eventqueue-0" java.lang.classcastexception
asked Jun 18 Chi Omega 168k points
0 votes
1 answer 42 views
42 views
Problem: Exception in thread "main" java.lang.numberformatexception: empty string.
asked May 6 Ryan evans 1.6k points