+ 1
Error in Program
https://code.sololearn.com/ceN5q84VCiJv/?ref=app Pls help me, I couldn't get it why this error is coming if anyone has any solution to the error then pls tell me :(
6 Respostas
+ 3
https://code.sololearn.com/cJIt2knE0HQi/?ref=app
I got my solution everyone
When I tried to do it by using io package, it ran perfectly
Stefanoo SoloProg José Ricardo Jiménez Aguilera just check it out
+ 1
Use
int n= Integer.parseInt(in.nextLine());
instead of
int n=in.nextInt();
+ 1
If your input is like this it works.
Input:
2 Name1
Name2
Ouput:
N
It's because your a still on the first line after nextInt and nextLine finds a Space as first char.
+ 1
That's because a String itself can throw this exception.
It's not the array who is throwing it.
And your first string at Index 0 is empty.
0
Exception in thread "main" java.lang.StringIndexOutOfBoundsException:
index out of range: 0 at java.base/
java.lang.StringLatin1.charAt(StringLatin1.java:48) at java.base/java.lang.String.charAt(String.java:711) at Program.main(Program.java:18)
I think the main problem is in line 18
Char ch=a[i].charAt(0);
It is saying the index of the string is not 0 or the compiler can't find any character in that position. But as a learner, I know every string starts with a position 0, but why this error is coming everytime I don't know.
0
Stefanoo sir, I couldn't get it! 😥