About Question enthuware.ocajp.i.v8.2.1225
Moderator: admin
-
- Posts: 2
- Joined: Sun Dec 04, 2016 1:31 am
- Contact:
About Question enthuware.ocajp.i.v8.2.1225
Hi,
the question item enthuware.ocajp.i.v8.2.1225 seems not correct.
The Option: "It throws StringIndexOutOfBoundsException if passed a value higher than or equal to the length of the string (or less than 0)." is a valid option.
The Java Docs (https://docs.oracle.com/javase/8/docs/api/) are not precise here. A StringIndexOutOfBoundsException will be thrown beside the fact that its base class is
of IndexOutOfBoundsException.
Anyway, the more specific solution (Option with StringIndexOutOfBoundsException) is treated as an error here, despite the fact that it is correct.
I am using Java 1.8.0_60 and checked also the jdk sources of the String class. Question Bank version is 1/44.
Maybe I missed something or the issue has been reported already. In that case I say "Sorry" for bothering you.
Happy day
STephan
the question item enthuware.ocajp.i.v8.2.1225 seems not correct.
The Option: "It throws StringIndexOutOfBoundsException if passed a value higher than or equal to the length of the string (or less than 0)." is a valid option.
The Java Docs (https://docs.oracle.com/javase/8/docs/api/) are not precise here. A StringIndexOutOfBoundsException will be thrown beside the fact that its base class is
of IndexOutOfBoundsException.
Anyway, the more specific solution (Option with StringIndexOutOfBoundsException) is treated as an error here, despite the fact that it is correct.
I am using Java 1.8.0_60 and checked also the jdk sources of the String class. Question Bank version is 1/44.
Maybe I missed something or the issue has been reported already. In that case I say "Sorry" for bothering you.
Happy day
STephan
-
- Site Admin
- Posts: 10046
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.1225
Please go through this discussion: viewtopic.php?f=2&t=1587&p=15856&hilit= ... sException
If you like our products and services, please help us by posting your review here.
-
- Posts: 2
- Joined: Sun Dec 04, 2016 1:31 am
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.1225
Appreciate your feedback. Thanks.
-
- Posts: 8
- Joined: Mon Jul 06, 2020 1:11 pm
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.1225
The Question is asking about if we can pass a char to charAt(). Now as per the information given by you it can, but till the compilation level. If it is the case can you please tell me what happens in the background if the statement is written as "Make it to 65 characters".charAt('A');
Is this tries to convert it into its ASCII value and search for the index position of the ASCII value? or something else!
Thanks in advance!
Is this tries to convert it into its ASCII value and search for the index position of the ASCII value? or something else!
Thanks in advance!
-
- Site Admin
- Posts: 10046
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.1225
Well, a char is an integral data type. So, you can pass 'A' to charAt. Integral value of 'A' i.e. 65 will be passed. It will be like calling charAt(65).
You can try printing the value of i after this statement and see what happens:
int i = 'A';
You can try printing the value of i after this statement and see what happens:
int i = 'A';
If you like our products and services, please help us by posting your review here.
-
- Posts: 8
- Joined: Mon Jul 06, 2020 1:11 pm
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.1225
Yes, I have tried this but even after having string value length as 104. I am getting this error.
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 104(Still why?)
The tested code is:
char checkChar = "This is a string of length 104".charAt('A');
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 104(Still why?)
The tested code is:
char checkChar = "This is a string of length 104".charAt('A');
-
- Site Admin
- Posts: 10046
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.1225
As mentioned above, charAt('A') is same as charAt(65). Do you have more than 65 characters in your string "This is a string of length 104" ? What do you think the character at index 65 is in your string?
If you like our products and services, please help us by posting your review here.
-
- Posts: 8
- Joined: Mon Jul 06, 2020 1:11 pm
- Contact:
Who is online
Users browsing this forum: No registered users and 92 guests