• Register
0 votes
203 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 19 views
19 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 8 views
8 views
How can I solve this?
asked Dec 30, 2020 TeamScript 10.5k points
0 votes
1 answer 7 views
7 views
How is this caused and how can I solve it?
asked 1 day ago TeamScript 10.5k points
0 votes
1 answer 835 views
835 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
1 vote
1 answer 75 views
75 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 11 views
11 views
Help me by solving this question.
asked Jan 4 TeamScript 10.5k points
0 votes
2 answers 248 views
248 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, 2020 mphil 2.3k points
0 votes
2 answers 280 views
280 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
0 votes
1 answer 31 views
31 views
Problem: Hi! I a working with a Desktop Application in C#. I am trying to run the following program: using System; using System.Windows.Forms; namespace multiple_conditions { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void okButton_Click( ... : 'Input string was not in a correct format.' I do not know what the problem is. Can anybody here help me?
asked Dec 4, 2020 Code Learner 5.5k points
0 votes
1 answer 16 views
16 views
Can anyone help me?
asked Jan 1 TeamScript 10.5k points