+ 2
[ANSWERED]StringIndexOutOfBoundsexception
https://code.sololearn.com/cYO0mObB0n3p/?ref=app 1. How to throw multiple exceptions? 2. Why do I get the error StringIndexOutOfBoundsexception? Sorry, the scanner and writer stuff doesnt work in the app, but on the PC it displays only the error mentioned.
10 Answers
+ 14
@Alexander 😁
Btw, the solution:
If the char is deleted, your line is shorter after that... I think you see the problem now ^^
+ 19
[Edited]
Correction:
for (int i = 0; i < line.length(); i++) {
for (int j = 0; j<arr.length; j++){
Explanation: if the array length is 5, you can access index from 0 to 4
+ 17
@Tashi, stop being so sweet 😑
+ 16
1. Throw multiple exceptions:
https://code.sololearn.com/cQVLlvKiWbiz/?ref=app
In case you mean catch multiple exceptions:
try {
// do something
} catch (NullPointerException npe) {
// error handling
} catch (StringIndexOutOfBoundsException) {
// error handling
}
Search for try / multicatch.
2. Best answer by Shamina ^^
+ 14
@Alexander
I'll have a closer look...
@Shamina 😊
+ 12
👍
+ 4
@Tashi
@Shamima
I now start to understand what was going on. I messed around with that a lot and found out that if I add to the word in the file a question mark, it does compile and outputs the word without the mark. In case such symbols like : or ; or ! or ( are used it displays the error. That's now interesting.
+ 1
A little story...
The teacher gave us a task to examine a book readability and one of the option was to find out how many words there are in the book that are more than 6 chars and less than 6. On the lesson I wrote a simple code not to read a random page and count them.
thought it would be nice to bring an analysis of the whole book. I was more or less satisfied with the code. Before sending the work I thought that symbols like semicolons, brackets, commas influence the result, more appropriate would be to count with the symbols. And now it is the 2nd hour I am struggling with it instead of just analyzing a random page of the book.
Coding does help in most of cases, but beware, guys and ladies...
+ 1
Now it works.
0
The array index out of bounds exception arises if you try to give a value to the index that was not in memory
int[] a=new int[5];//it creates an //array of index 5 i.e., 0,1,2 3,4.
a[5]=15;//it caught error,because //array index starts with 0 and //ends with 4 if you create a 5 //spaces of array i.e.,index of num //5 is not existed.but you gave a //value of 15 to it.then it got array //index out of bounds exception //error