I am a beginner coder and I came across this problem, I had referred other codes that had the same problem and tried the solutions but they didn't work. The code is:
import java.util.Scanner;
public class ShortName
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter a string");
String str = sc.nextLine();
str.trim();
int spaceOne = str.indexOf(' ');
int lastSpace = str.lastIndexOf(' ');
System.out.print(str.substring(0, spaceOne) " ");
//System.out.print(str.substring(lastSpace, str.length()) " ");
String remain = str.substring(spaceOne, lastSpace);
remain = remain " ";
for(int i = 0; i < remain.length(); i )
{
if(remain.charAt(i)==' ')
System.out.print(remain.charAt(i 1) ". ");
}
System.out.print(str.substring(lastSpace, str.length()) " ");
}
}
And the error I get after entering the input as 'Mathew Mark Luke John' is:
Mathew M. L. Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 11
at java.base/java.lang.StringLatin1.charAt(StringLatin1.java:48)
at java.base/java.lang.String.charAt(String.java:712)
at ShortName.main(ShortName.java:24)
PS C:\Users\Rianette\Desktop\10th comp programs>
The output should be
Mathew M. L. John
Please help me.
Thanking you, RonJ
CodePudding user response:
You are getting String Index Out of Bound Exception (SIOBE) when you try to access a character at an index which is greater than the length of the string. In your example, when you are printing
System.out.print(remain.charAt(i 1) ". ");
this is where your code gets SIOBE as when i = remain.length() -1 and in that case, it will try to access charAt at remain.length() which will give the above exception. To sort it, you can put an conditional check to ensure that i 1 < remain.lenght() -1
Thus , your final code becomes like this :
import java.util.Scanner;
public class ShortName {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter a string");
String str = sc.nextLine();
str.trim();
int spaceOne = str.indexOf(' ');
int lastSpace = str.lastIndexOf(' ');
System.out.print(str.substring(0, spaceOne) " ");
// System.out.print(str.substring(lastSpace, str.length()) " ");
String remain = str.substring(spaceOne, lastSpace);
remain = remain " ";
for (int i = 0; i < remain.length(); i ) {
if (remain.charAt(i) == ' ')
if (i 1 < remain.length() - 1) {
System.out.print(remain.charAt(i 1) ". ");
}
}
System.out.print(str.substring(lastSpace, str.length()) " ");
}
}
and the output is like this :
Enter a string
Mathew Mark Luke John
Mathew M. L. John
CodePudding user response:
What is StringIndexOutOfBoundsException?
StringIndexOutOfBoundsException while using String.charAt(index) occurs when you try to access an index of string that is not contained in the string. Say you have a string "Hello". And you perform "Hello".charAt(5), you get the same exception. Why? Because this string has index 0 to 4, and you are accessing an index 5.
When you enter 'Mathew Mark Luke John',
System.out.print(str.substring(0, spaceOne) " ");
This line prints Mathew
String remain = str.substring(spaceOne, lastSpace);
remain = remain " ";
These two lines initialise remain as Mark Luke. Notice there are spaces at both beginning and end.
Now you are looping through the character values of remain. The loop will run from 0 to remain.length -1;
When i = remain.length -1, you perform remain.charAt(i 1). Therefore you get error because indexes run from 0 to remain.length -1.
