• Register
0 votes
161 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

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 8 views
8 views
I need to find frequency of charts in a string? can anyone here help me?
asked Sep 22 Daniel Anderson 4k points
0 votes
1 answer 495 views
495 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 jwilliam 3.9k points
1 vote
1 answer 37 views
37 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 Martin K 6.6k points
0 votes
2 answers 107 views
107 views
Problem : Currently I am working on my app for Android but I am getting following error while working with the SharedPreferences. I am very new to Android App Programming. I am trying to debug it from past 2 days but unable to find the solution ... " android:inputType="number" android:key="capitalownership" android:title="@string/capitalownership" /> Can somebody help me in resolving this error?
asked Feb 17 mphil 2.3k points
0 votes
2 answers 108 views
108 views
Problem : I got bellow message from my code Exception in thread "main" java.lang.ArithmeticException: / by zero
asked Nov 1, 2019 peterlaw 6.9k points