What is the value in the variables x, y, and z after the following statements execute (assume each variable below has already been declared)?

str = "the Bluenose in the sun";
x = str.substring(4, str.indexOf("in"));
y = x.length();
z = x.charAt(4);

find the answers for the questions below:
Value of x:
Value of y:
Value of z:

read the documentation of str.substring, and things should be clear.

Remember that strings are indexed starting at zero.
There are lots of examples online. See what you come up with.

for x, i got: Bluenose

for y, i got: 9
for z, i got: 110

my assignment says that x, y and z is wrong. but i copied it this java program in my java editor and and it gave x = Bluenose, y =9 and z= 110

for x, you will also get the trailing space

y is correct, oddly enough, since the length of "Bluenose" is only 8.
z is a char, not an int

thanks oobleck

To find the values of variables x, y, and z after executing the given code, we need to understand each statement and perform the necessary operations.

Let's break it down step by step:

1. `str = "the Bluenose in the sun";`
- This line assigns the string "the Bluenose in the sun" to the variable `str`.

2. `x = str.substring(4, str.indexOf("in"));`
- Here, we are assigning the substring of `str` starting from index 4 (inclusive) to the index of the first occurrence of "in" (exclusive) to the variable `x`.
- The substring " Bluenose " will be extracted and assigned to `x`.

3. `y = x.length();`
- This line calculates the length of the string stored in variable `x`.
- The value of y will be the number of characters in the string " Bluenose ", which is 10.

4. `z = x.charAt(4);`
- Here, we assign the character at index 4 of the string stored in variable `x` to the variable `z`.
- The value of z will be the character 'u' (the fifth character in " Bluenose ").

Therefore, the answers to the questions are:

Value of x: " Bluenose "
Value of y: 10
Value of z: 'u'