• Register
0 votes
261 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 5 3
6,930 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

9 7 4
38,600 points
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.

10 6 4
31,120 points

Related questions

0 votes
1 answer 13 views
13 views
Problem: java lang stringindexoutofboundsexception string index out of range 0
asked Feb 23 Hira 2.2k points
0 votes
1 answer 26 views
26 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.1K views
1.1K 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 26 views
26 views
How can I solve this?
asked Dec 30, 2020 TeamScript 13.5k points
0 votes
1 answer 37 views
37 views
How is this caused and how can I solve it?
asked Jan 22 TeamScript 13.5k points
1 vote
1 answer 105 views
105 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 3 views
3 views
Problem: I need someone&rsquo;s help to fix it, out of range exception c++
asked Mar 22 Ifra 24.4k points
0 votes
1 answer 29 views
29 views
It looks kinda like it's trying to access the last character of something and that's upsetting it, but I have no idea why. Any help for a total newbie?
asked Feb 12 TeamScript 13.5k points
0 votes
1 answer 2 views
2 views
Problem: Hello ... Can anyone tell me that why I got this error? exception: java.lang.nullpointerexception ejbclass="productratesservices.getserviceoptions"
asked Apr 1 Ifra 24.4k points
0 votes
1 answer 7 views
7 views
Problem: Any ideas on what I need to fix the error: could not find or load main class main caused by: java.lang.classnotfoundexception: main?
asked Mar 27 ashik 11.2k points